0

我试图让这个函数为其“this”运算符获得正确的范围,但没有运气。在AssetName = function(options){代码块内,我希望“this”指向 class AssetName。我错过了什么?权利的范围this从一开始就是window

Assetname: function(options){

    var Base = WM.Utility.GenericFilter()
    options = options;

    if (typeof Object.create !== "function") { 
        // For older browsers that don't support object.create
        Object.create = function (o) {
            function F() {}
            F.prototype = o;
            return new F();
        };
    }       

    var AssetName = {};
    AssetName = function(options){
        return function(){
            var self = this;

            debugger;
            // Call the super constructor.
            Base.call(this, options);

            this.$mod.on('change', '#asset-name-quick-search', self, 
                this.search);
            this.$mod.on('click', '.close', self,  this.remove);

            this.initTypeAhead();
            this.$selectionList = this.$mod.find("#asset-name-selection-list");

            this.assetListItems = [];

            return this;    
        }(options, AssetName);

    }

    // The AssetName class extends the base GenericFilter class.
    AssetName.prototype = Object.create(Base.prototype);

    AssetName.prototype.initTypeAhead = function(){
        var options = {};
        options.source = _.pluck(this.collection, 'asset_name');
        options.items = 8;
        this.$mod.find('#asset-name-quick-search').typeahead(options);    
    };

    AssetName(options);
    return AssetName;
},
4

2 回答 2

1
AssetName = function(options){
        return function(){
            var self = this;

            debugger;
            // Call the super constructor.
            Base.call(this, options);

            this.$mod.on('change', '#asset-name-quick-search', self,  this.search);
            this.$mod.on('click', '.close', self,  this.remove);

            this.initTypeAhead();
            this.$selectionList = this.$mod.find("#asset-name-selection-list");

            this.assetListItems = [];

            return this;    
        }(options, AssetName);

    }

改成

AssetName = function(options){
        var aa =  function(){
            var self = this;

            debugger;
            // Call the super constructor.
            Base.call(this, options);

            this.$mod.on('change', '#asset-name-quick-search', self,  this.search);
            this.$mod.on('click', '.close', self,  this.remove);

            this.initTypeAhead();
            this.$selectionList = this.$mod.find("#asset-name-selection-list");

            this.assetListItems = [];

            return this;    
        };
        aa.call(AssetName,options);

    }

在您的代码中,该函数按aa原样调用。aa(options);thiswindow

[更新]

我用以下代码修复了这个错误:

AssetName =  function (options) {
    AssetName = function (options) {
        var aa = function () {
            alert(this);
            return this;
        };
        aa.call(this, options);
    }
    AssetName.prototype.initTypeAhead = function () {
        alert(1);
    }

    return new AssetName(options);;
};
var test = AssetName();
test.initTypeAhead();

但我建议如何编写如下代码:

AssetName =  function (options) {
    AssetName = function (options) {
            alert(this);
    }
    AssetName.prototype.initTypeAhead = function () {
        alert(1);
    }

    return  new AssetName();
};
var test =  AssetName();
test.initTypeAhead();
于 2013-01-18T18:00:46.323 回答
0

您只需将您var self = this的匿名返回函数移出即可。然后你可以使用只使用self.

于 2013-01-18T18:02:44.927 回答