繁体   English   中英

为什么这些JavaScript函数未添加到该对象?

[英]Why these JavaScript functions don't add to this object?

我想为基于Json文件的对象编写getter和setter函数。 我复制了约翰·雷西格(John Resig)的书的以下代码(Appress Pro JavaScript Techniques),但是它不起作用,并且这些函数没有添加到对象中。您能告诉我为什么,什么是正确的代码?

// Create a new user object that accepts an object of properties
function User(properties) {
// Iterate through the properties of the object, and make sure
// that it's properly scoped (as discussed previously)
  for (var i in properties) {
    (function () {
        // Create a new getter for the property
        this["get" + i] = function () {
            return properties[i];
        };

        // Create a new setter for the property
        this["set" + i] = function (val) {
            properties[i] = val;
        };
    })();
  }
}

// Create a new user object instance and pass in an object of
// properties to seed it with
var user = new User({
name: "Bob",
age: 44
});

// Just note that the name property does not exist, as it's private
// within the properties object
//alert( user.name == null );

// However, we're able to access its value using the new getname()
// method, that was dynamically generated
alert(user.getname());

您已经使用一个函数创建了一个闭包,但是您忘记了将i传递给它。 您还需要在函数内部this进行不同的引用,因为上下文将其更改为window

function User(properties) {
    var i, me = this;
    for (i in properties) (function (i) {
        // Create a new getter for the property
        me["get" + i] = function () { // using `me` because `this !== me`
            return properties[i];
        };

        // Create a new setter for the property
        me["set" + i] = function (val) {
            properties[i] = val;
        };
    }(i)); // i passed into function closure
}

在您的IIFE中, this实际上是window 您必须使用Function.prototype.call来指定上下文:

function User(properties) {
    // Iterate through the properties of the object, and make sure
    // that it's properly scoped (as discussed previously)
    for (var i in properties) {
        (function(i) {
            // Create a new getter for the property
            this["get" + i] = function() {
                return properties[i];
            };

            // Create a new setter for the property
            this["set" + i] = function(val) {
                properties[i] = val;
            };
        }).call(this, i);
    }
}

或保留另一个变量的引用:

function User(properties) {
    var that = this;

    // Iterate through the properties of the object, and make sure
    // that it's properly scoped (as discussed previously)
    for (var i in properties) {
        (function(i) {
            // Create a new getter for the property
            that["get" + i] = function() {
                return properties[i];
            };

            // Create a new setter for the property
            that["set" + i] = function(val) {
                properties[i] = val;
            };
        })(i);
    }
}

如果处于严格模式,则代码将引发错误,而不是误导成功:

TypeError: Cannot set property 'getname' of undefined

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM