将' this '的继承扩展到' object '的方法/属性

Extending inheritance of `this` to methods / properties of an `object`

本文关键字:方法 属性 object 扩展到 继承 this      更新时间:2023-09-26

我不确定我是否正确地表达了问题标题;请考虑以下事项以澄清…

(function() {
    var foo = {
        bar: function() {
            // Is it possible to reference 'this' as the
            // initializing 'object' aka 'e' and not 'foo' ?
            // The easy part, currently because 'this' refers to 'foo',
            // is returning 'this' aka 'foo' so that chaining can occur
            return this;
        },
        other: function() {
            return this;
        }
    };
    Event.prototype.foo = foo; 
}());
// usage
document.onmousemove = function(e) {
    e.foo.bar().other();
};

我如何在foo的方法/道具中访问this,但this指的是初始的objecte而不是foo ?


我想出的最好的办法是这个

(function() {
    var foo = function() {
        var _foo = this.foo;
        _foo._this = this; //recursive reference that I am VERY worried about
        return _foo;
    };
    foo.bar = function() {
        var _this = this._this; //_this refers to initial 'object', 'e'
        return this; //return 'foo' aka 'this' for function chaining
    };
    foo.other = function() {
        var _this = this._this;
        return this;
    };
    Event.prototype.foo = foo; 
}());
// usage
document.onmousemove = function(e) {
    e.foo().bar().other();
};

我目前的工作,但我担心一些事情…

1. e赋值给e.foo._this的递归引用



2. 将e分配给e.foo._this的冗余性,如果this可以作为e而不是foo访问,它将使"事物"更具性能,特别是关于像鼠标移动事件这样的事情。

jsFiddle这里


同时,我试图避免这样的事情…

document.onmousemove = function(e) {
    e.foo.bar.call(e);
};

感谢您的宝贵时间。

只要稍微改变一下,就可以让事情变得更简单:

(function() {
    var foo = function() {
      this.foo.event = this;
      return this.foo;
    };
    foo.bar = function() {
      /// the event can be found in this.event
      return this;
    };
    foo.other = function() {
      /// the event can be found in this.event
      return this;
    };
    Event.prototype.foo = foo;
}());
// usage
document.onmousedown = function(e) {
    e.foo().bar().other();
};

然而,这是对共享对象foo的更改,您可能希望重写一些东西,以便e.foo()返回foo的新实例,并将其他方法移动到foo's原型。

(function() {
    var foo = function(event) {
      this.event = event;
    };
    foo.prototype.bar = function() {
      /// the event can be found in this.event
      return this;
    };
    foo.prototype.other = function() {
      /// the event can be found in this.event
      return this;
    };
    Event.prototype.foo = function() {
      return new foo(this);
    };
}());

这样你每次都创建一个新的foo实例,但这意味着你添加的event属性是本地化到该实例的;原型方法将在所有实例中共享,所以从优化的角度来看,这并不是太糟糕。

也许这对你有用:

使用apply方法更改被调用方法中的this上下文,并使用this.foo引用foo:

(function () {
    var foo = function () {
        console.log(this);
        return this.foo;
    };
    foo.bar = function () {
        console.log(this);
        return this.foo;
    };
    foo.other = function () {
        console.log(this);
        return this.foo;
    };
    Event.prototype.foo = foo;
}());
// usage
document.onclick = function (e) {
    console.log(
        e.foo.apply(e).bar.apply(e).other.apply(e)
    );
};

小提琴

也许将函数绑定到它的对象会更简单:

someElement.onEvent = myObject.myHandlerFunction.bind(myObject);

所以当这个函数被调用时,它的this将是myObject。