Javascript 如何将对象属性作为参数传递?
例如,假设我使用节点分类拥有此函数:Javascript 如何将对象属性作为参数传递?,javascript,jquery,Javascript,Jquery,例如,假设我使用节点分类拥有此函数: function example(){ var exampleVar = nodes.classfication; //below i use exampleVar to do calculations . . . . } 但有时我想获取节点状态,所以我会这样做: function example(){ var exampleVar = nodes.status; //below i use exampleVar to do cal
function example(){
var exampleVar = nodes.classfication;
//below i use exampleVar to do calculations
.
.
.
.
}
但有时我想获取节点状态,所以我会这样做:
function example(){
var exampleVar = nodes.status;
//below i use exampleVar to do calculations
.
.
.
.
}
我不想重复代码,因为(在我的代码中)函数中有很多代码需要重用。因此,根据我想从这个函数中得到什么,我将它传递给函数,如下所示:
function example(thisAttribute){
var exampleVar = nodes.thisAttribute; //using the variable passed to the function
//below i use exampleVar to do calculations
.
.
.
.
}
这行不通,如何将对象属性作为参数传递给函数?我试着在网上查找,但我不认为我在搜索正确的东西,因为我找不到任何帮助。无论如何,请提前感谢:)使用:
相关的,潜在的重复:啊,很好,我不知道。非常感谢
function example(thisAttribute){
var exampleVar = nodes[thisAttribute];
}