Javascript 将“this”的继承扩展到“object”的方法/属性`
我不确定我是否正确地表达了问题的标题;请考虑以下事项以澄清……/P>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
(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
引用初始对象
akae
而不是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的解决方案就是这样的。这是一个很好的解决方法。