JavaScript 方法链接和 this 对象
javascript method chaining and the this object
给定一个链式函数,声明如下:
Something.prototype.method1.method2 = function(){
return this === Something.prototype.method1; // true
}
如何(如果可能(访问调用实例对象?
instance.method1.method2() // access to instance and method1?
如果你真的想滥用JS,请忽略我的回答。
否则,让我展示一下链接函数的想法是关于什么的。原型函数可以相互使用,因为它们始终返回this
指针。因此,链中的下一个函数将在同一对象上调用:
Animal = function Animal(name) {
this.food = 0;
this.name = name;
return this;
}
Animal.prototype.eat = function eat() {
this.food--;
return this;
}
Animal.prototype.hunt = function hunt() {
this.food++;
return this;
}
现在,您可以执行以下操作:
// Lets create Jack the cat.
var cat = new Animal('Jack');
// Let him hunt and eat with some chained function calls.
cat.hunt().hunt().eat();
// Check how much food does Jack got.
console.log(cat.food) // => 1
// Some explanation about this return values.
var anotherPointerOnCat = cat.hunt();
console.log(anotherPointerOnCat === cat) // => true
// True because they point on the same object in memory.
// So I can call the chained functions on that as well.
anotherPointerOnCat.eat().eat();
您需要在构造过程中定义它:
function Thing() {
var _t = this;
this.method1 = function() {
console.log('method1', _t);
}
this.method1.method2 = function() {
console.log('method1.method2', _t);
}
}
在 Chrome 控制台中工作:
var t1 = new Thing();
t1.a = 1;
t1.method1();
t1.method1.method2();
var t2 = new Thing();
t2.b = 2;
t2.method1();
t2.method1.method2();
收益 率:
method1 "Thing {method1: function, a: 1}"
method1.method2 "Thing {method1: function, a: 1}"
method1 "Thing {method1: function, b: 2}"
method1.method2 "Thing {method1: function, b: 2}"
您可以将对象设置为链对象,即将对象设置为接受一个可以是父级的参数,并让它返回一个不同的对象,但具有相同的链。
偷@burninggramma的例子:
function buildChain(parent, method){
var child = {};
child.prototype = parent;
child.parentage = function(){
var chain = [];
if (child.prototype.parentage) {
chain = child.prototype.parentage();
}
chain.push({obj:this, method:method});
return chain;
}
}
function Animal(name){
this.food = 0;
this.name = name;
return this;
}
Animal.prototype.eat = function eat() {
this.food--;
return buildChain(this, this.eat);
}
Animal.prototype.hunt = function hunt() {
this.food++;
return buildChain(this, this.hunt);
}
并使用:
var cat = new Animal('Tom');
var mouse = new Animal('Jerry');
cat.hunt().hunt().eat().parentage(); // [{obj: cat, method: hunt}, {obj: chain1, method: hunt}, {obj: chain2, method: eat}]
mouse.eat().eat().parentage(); // [{obj: mouse, method: eat}, {obj: chain1, method: eat}]
var chainer = cat.hunt().eat();
chainer.parentage(); // [{obj: cat, method: hunt}, {obj: chain1, method: eat}]
chainer.hunt().parentage(); // [{obj: cat, method: hunt}, {obj: chain1, method: eat}, {obj: chain2, method: hunt}]
chainer.eat().parentage(); // [{obj: cat, method: hunt}, {obj: chain1, method: eat}, {obj: chain2, method: eat}]
Something.prototype.method1.method2 = function(){
return this === Something.prototype.method1; // true
}
您声称此函数将始终返回 true。但是,这不是真的。举个例子:
var jsIsFun = Something.prototype.method1.method2;
jsIsFun(); //false
jsIsFun.call(Something.prototype.method1); //true
这是因为 this
关键字在函数执行时绑定。但是,使用call()
方法,我们可以更改绑定到this
的内容。
发生这种情况是因为函数的上下文在执行时应用。
但是,您希望访问不直接在对象/函数中的变量。
我们对此也有一个词:范围。
您遇到的问题是不了解 Scope 及其在 JS 中的工作方式。
相关文章:
- 警告 - 全局 THIS 对象的危险使用
- 为什么当我尝试将函数绑定到自身时,“this”对象仍然引用窗口
- 在茉莉花中传递“this”对象流星方法
- Jquery $.ajax 获取响应与将“this”对象嵌套传递到“success”回调函数冲突
- 如何将事件的“this”对象更改为其他函数
- 如何通过锚点链接将“this”对象传递给函数,并将其转换为jQuery对象
- 如何使用绑定事件在按钮单击时获取当前/this对象
- 存储'this'对象中的onclick属性
- AngularJs——设置'this'对象在控制器中的$http调用
- 为什么不是在 JavaScript 中匿名函数之外访问“this”对象的关键字
- 为什么调用array.prototype. foreach .call()时将数组设置为THIS对象不能工作?
- JavaScript 方法链接和 this 对象
- 使用'this'对象构造函数中的变量
- 由事件监听器绑定的函数如何继承' this '对象?
- 如何访问“this”;对象
- while循环和"this"对象
- Javascript send "this"对象作为参数
- 使用“this"对象方法内部的关键字
- Onmouseover和内联函数使用'this'对象
- 使用"for "循环全局"this"对象