1

我正在学习 OO JavaScript,所以这个问题可能听起来很奇怪。问题normalize函数应该是私有的,外部不可访问/可写。问题:如何从原型函数normalize内部访问?prepare

var AuthHmac = AuthHmac || (function(_, CryptoJS) {
    function AuthHmac(options) {
        var options = options || {},
            normalize = function(s) { return s.toLowerCase(); };

        this.additionalHeaders = options.additionalHeaders || {};
    };

    AuthHmac.prototype.prepare = function(request) {
        request.headers = request.headers || {};

        _.each(this.additionalHeaders, function(value, name) {
            request.headers[this.normalize(name)] = value;
        });
    };

    return AuthHmac;

})(_, CryptoJS);
4

4 回答 4

1

不,如果该normalize函数的范围仅限于构造函数,则不是。

但是,由于您的函数不需要特权(即可以访问构造函数的局部变量),您可以轻松地将它放在外面,它仍然是您的模块私有的:

var AuthHmac = AuthHmac || (function(_, CryptoJS) {

    function normalize(s) { return s.toLowerCase(); }

    function AuthHmac(options) {
        var options = options || {};

        this.additionalHeaders = options.additionalHeaders || {};
    };

    AuthHmac.prototype.prepare = function(request) {
        request.headers = request.headers || {};

        _.each(this.additionalHeaders, function(value, name) {
            request.headers[normalize(name)] = value;
//                          ^^^^^^^^^
//    No `this.`! "private attributes" in JS are variables, not properties
        });
    };

    return AuthHmac;

})(_, CryptoJS);
于 2013-02-08T14:27:23.013 回答
1
var AuthHmac = AuthHmac || (function(_, CryptoJS) {

function normalize(s) { return s.toLowerCase(); }

function AuthHmac(options) {
    var options = options || {};

    this.additionalHeaders = options.additionalHeaders || {};
};

AuthHmac.prototype.prepare = function(request) {
    request.headers = request.headers || {};

    _.each(this.additionalHeaders, function(value, name) {
        request.headers[normalize(name)] = value;
    });
};

return AuthHmac;

})(_, CryptoJS);
于 2013-02-08T14:27:32.903 回答
1

你不能。JavaScript 没有任何私有或公共属性的概念,因此人们使用闭包和作用域作为“hack”来模拟隐私。但这是有代价的,即原型方法无法访问这些“私有”属性。

但是,在您的具体示例中,normalize也可以在构造函数之外定义,因为它不依赖于构造函数内部的任何内容:

var AuthHmac = AuthHmac || (function(_, CryptoJS) {

    var normalize = function(s) { return s.toLowerCase(); };

    function AuthHmac(options) {
        var options = options || {};
        this.additionalHeaders = options.additionalHeaders || {};
    };

    AuthHmac.prototype.prepare = function(request) {
        request.headers = request.headers || {};

        _.each(this.additionalHeaders, function(value, name) {
            request.headers[normalize(name)] = value;
        });
    };

    return AuthHmac;

})(_, CryptoJS);
于 2013-02-08T14:27:42.023 回答
1

将函数定义normalise为范围内的命名函数,而不是将其放在对象的属性中:

var AuthHmac = AuthHmac || (function(_, CryptoJS) {

  function normalize(s) {
    return s.toLowerCase();
  }

  function AuthHmac(options) {
    var options = options || {};

    this.additionalHeaders = options.additionalHeaders || {};
  };

  AuthHmac.prototype.prepare = function(request) {
    request.headers = request.headers || {};

    _.each(this.additionalHeaders, function(value, name) {
        request.headers[normalize(name)] = value;
    });
  };

  return AuthHmac;

})(_, CryptoJS);
于 2013-02-08T14:28:47.980 回答