在javascript中管理此范围

时间:2013-01-18 17:48:09

标签: javascript jquery

我正在尝试使用此函数来获取其“this”运算符的正确范围,但没有运气。在AssetName = function(options){代码块中,我希望“this”指向类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;
},

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

您只需移动匿名返回功能的var self = this。然后你可以使用self