Reference a javascript property with a dynamic name

后端 未结 3 1917
北海茫月
北海茫月 2021-01-27 12:09

I\'m using Vue.js and I am wondering how I can call a javascript property in a dynamic way. Let\'s say I pass an argument to a function like this:

filter: functi         


        
3条回答
  •  暖寄归人
    2021-01-27 12:37

    The only way I can imagine this working is if you pass a string to the function, and interpolate that string into the object-notation, using square-brackets:

    filter: function(keyName) {
        this[keyName + 'ToFilter']splice(index, 1);
    }
    

    If your existing variable, items (from the question), contains a value of relevance within the function then you'd also need to pass that as an argument also:

    filter: function(keyName, items) {
        this[keyName + 'ToFilter']splice(index, 1);
    }
    

提交回复
热议问题