我有一个简单的对象,我想通过函数传递属性名称来调用特定属性:
spit(name);
function spit(value) {
var test = new Object();
test.name = "Bill";
test.number = 24;
console.log(test.value);
}
上面的代码应该返回“Bill”。这怎么可能?
最佳答案
听起来您正在寻找这样的东西:
spit('name');
function spit(value) {
var test = new Object();
test.name = "Bill";
test.number = 24;
console.log(test[value]);
}
关于javascript - 如何通过javascript中的函数返回对象属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8175895/