0

この関数を取得して、「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;
},
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 に答える