Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将“this”的继承扩展到“object”的方法/属性`_Javascript_Inheritance_Methods_This - Fatal编程技术网

Javascript 将“this”的继承扩展到“object”的方法/属性`

Javascript 将“this”的继承扩展到“object”的方法/属性`,javascript,inheritance,methods,this,Javascript,Inheritance,Methods,This,我不确定我是否正确地表达了问题的标题;请考虑以下事项以澄清……/P> (function() { var foo = { bar: function() { // Is it possible to reference 'this' as the // initializing 'object' aka 'e' and not 'foo' ? // The easy part, currently b

我不确定我是否正确地表达了问题的标题;请考虑以下事项以澄清……/P>
(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
引用初始
对象
aka
e
而不是
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.的递归引用
和

2.将
e
分配给
e.foo的冗余。如果
e
可以作为
e
而不是
foo
访问,则会使“东西”更有效,尤其是在鼠标移动事件方面


还有,我在努力避免类似的事情

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


非常感谢所有建议,谢谢您的时间。

也许这对您有用:

使用
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


然后,您可以使用e.target访问元素。

通过对现有内容的细微更改,您可以使事情变得更简单:

(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的
原型

(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
属性将本地化到该实例;原型方法将在所有实例中共享,因此从优化的角度来看并不太糟糕。

因此,如果我理解正确,您基本上希望同时访问
foo
对象和触发事件的元素?@basilikum Yes。。。有点?
事件本身和
foo
对象。有点像
this
this.foo
。“这有意义吗?”特里不担心。在JavaScript中找到解决方案的方法总是有很多种,这就是为什么我如此喜欢它作为一种语言的原因;)点头表示同意。也许这是“新奇”效应仍在蓬勃发展,因为我对JS还是新手,但我不这么认为。这是一种非常有表现力的语言,我喜欢学习它。谢谢你的帮助+1票。我知道乞丐不应该是挑肥拣瘦的人,但我希望实现更包容的目标(如果这个词正确的话?),然后多次
call
apply
调用。不管怎样,还是很棒,谢谢basilikum ^ ^谢谢!:)pebbl的解决方案就是这样的。这是一个很好的解决方法。