如何传递对象属性作为参数?

那一个人

例如说我有使用节点分类的这个功能:

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];
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章