添加一个新的 属性 inside object 而不是直接添加它?
Add a new property inside object without directly adding it?
我有这个代码:
var a = {
b: "Hello",
c: "Foo",
d: "Bar"
};
if(a["b"]) {
// this code will add a new property inside the object a
}
如何添加新的 属性 而不是直接将其添加到 a
对象中?我的意思是,在创建对象后在其中添加一个新的 属性 ?像这样。
function addNewPropertyInsideObject(obj,propname,propvalue) {
// some code here
}
var a = {
b: "Hello",
c: "Foo",
d: "Bar"
};
addNewPropertyInsideObject(a, e, "Baz"); // this code will add a new property with the name "e" & value "Baz" inside the object "a"
就这么简单
function addNewPropertyInsideObject(obj,propname,propvalue) {
obj[propname] = propvalue;
return obj;
}
我有这个代码:
var a = {
b: "Hello",
c: "Foo",
d: "Bar"
};
if(a["b"]) {
// this code will add a new property inside the object a
}
如何添加新的 属性 而不是直接将其添加到 a
对象中?我的意思是,在创建对象后在其中添加一个新的 属性 ?像这样。
function addNewPropertyInsideObject(obj,propname,propvalue) {
// some code here
}
var a = {
b: "Hello",
c: "Foo",
d: "Bar"
};
addNewPropertyInsideObject(a, e, "Baz"); // this code will add a new property with the name "e" & value "Baz" inside the object "a"
就这么简单
function addNewPropertyInsideObject(obj,propname,propvalue) {
obj[propname] = propvalue;
return obj;
}