Javascript链接模式返回该对象的等价项,而不是返回this
Javascript chaining pattern return equivalent of this object instead of returning this
当通过链接模式实现函数时,如果函数由外部源(不可靠的源)使用,则不建议返回"this"对象。相反,应该像下面的代码中那样返回等价的"this"。
var calc = function (start){
var that = this;
this.add = function(x){
start = start + x;
return that;
};
this.multiply= function(x){
start = start * x;
return that;
};
};
返回"that"而不是"this"有什么区别,因为它们都是相同的引用。
因为JavaScript中的this
是调用给定函数的对象。这在JavaScript 中的工作原理
相关文章:
- React+Meteor:this.ops返回未定义
- this内部的值返回this.each(function(){})
- 使用this和onclick()时不能返回id值
- this.defaultValue 返回 select 元素的未定义
- jQuery过滤器,返回不同的jQuery对象(即$(this).Pparent())
- jQuery插件返回this.each并为每个对象添加函数属性
- $(this)返回'未定义'
- jQuery不使用“$(this)”返回必需的元素
- $(this).val() 返回不正确的值 IE8
- “this”返回的是[对象窗口],而不是元素
- this.userId 在 Meteor.publish 中返回未定义
- Sencha ExtJs 为什么从“构造函数”方法返回“this”
- 当返回另一个构造函数时,从Javascript构造函数返回this的值
- 什么是原型,$.扩展,并返回this"不得不这么做
- 理解在timer.js中返回this
- JavaScript:寻找返回'this'而不是'val=x'
- jQuery插件返回this.each
- '返回this'在增加类型时执行
- Javascript链接模式返回该对象的等价项,而不是返回this
- DRY:使所有函数都返回this