javascript - 在功能 block 中管理特殊 "this"对象的属性

标签 javascript typescript

我在一个函数中得到了这段代码。

第一行有效。

    this.learningPlan.learnerType = ['abc']; // this line works

但下面的这些行不会。

    properyName = 'learningPlan.learnerType';
    this[properyName] = ['abc'];

有没有办法引用 this 以便我可以将 properyName 值传递给函数?

我需要这个功能,这样我就可以保持函数的通用性,而不用提及特定的短语“learningPlan.learnerType”。因为然后我可以执行以下操作:

function blah(propertyName){
        this[properyName] = ['abc'];
}

最佳答案

这看起来如何:

编辑:添加了解释性注释。

function T() {
    this.learningPlan = { learnerType : "aaa" };
    this.setVal = function(p, v) {
        // Split the property string into its "parts"
        // thus "learningPlan.learnerType" becomes ["learningPlan","learnerType"]
        var ps = p.split(".");

        // Remove and capture the "last" (right-most) property
        // From ["learningPlan","learnerType"] this would be "learnerType"
        // leaving the array with ["learningPlan"]
        var lastProp = ps.pop();

        // Work "down" the object finding each level object finally returning
        // the property object containing the property you wish to set
        var o = ps.reduce(function(a,e) { 
          // This checks that each object["property"] exists and if
          // it doesn't then it creates an empty object so that it can
          // continue traversing down the property hierarchy. 
          // Given the property string "learningPlan.learnerType" if the
          // property this.learningPlan didn't exist then attempting to 
          // reference this.learningPlan.learnerType would throw an error
          // since learnerType is not a property of undefined. This is, I
          // believe, the error you are experiencing. Using (a[e] || {}) 
          // ensures that a[e] references something so that traversal
          // can continue.
          a[e] = (a[e] || {}); 

          // This simply returns the property so that the reduce can keep 
          // moving through the array
          return a[e]; 
        }, this);

        // Finally, we set the value for the last property in our string.
        // The above reduce would walk down 'this' finding/setting each 
        // property until it reached the last string we left in our array
        // after the pop and thereby returning a reference to that property
        // into our variable "o". We then use the last property string as a
        // property of "o" to set our value. 
        o[lastProp] = v;
    };
}

它将允许您执行以下操作:

var t = new T();
t.setVal("learningPlan.learnerType", "def");

关于javascript - 在功能 block 中管理特殊 "this"对象的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47686336/

相关文章:

javascript - parsley.js - 日期字段的自定义验证

javascript - 使用 Calendar Extender 和 Javascript ASP.NET 验证日期

javascript - 滚动到页面底部并无限滚动

javascript - 找不到模块,您是说 "*js"吗?

javascript - 在将参数传递给去抖函数时限制或去抖 Vue 2 中的异步调用

javascript - JS - 无法获取函数实例的构造函数名称

javascript - 在 Observables 中操作数据的模式是什么(Angular2)

javascript - 如何在运行时合并两个数组

reactjs - 禁用 create-react-app 提供的 ESLint

javascript - Azure Devops 小部件使用 azure-devops-extension-api 按项目获取所有 Pull 请求,仅获取一个结果