return this'在函数.间接使用时的原型定义
What is the utility of the 'return this' in Function.prototype definition when used indirectly?
有四种情况
案例1:
Function.prototype.method = function(name, func){
this.prototype[name] = func;
//return this;
};
Number.method("testMethod",function(){
//return Math[ this < 0 ? "ceil" : "floor" ](this);
});
console.log(typeof (9.3).testMethod()); //outputs undefined
案例2:
Function.prototype.method = function(name, func){
this.prototype[name] = func;
//return this;
};
Number.method("testMethod",function(){
return Math[ this < 0 ? "ceil" : "floor" ](this);
});
console.log(typeof (9.3).testMethod()); //outputs number
案例3:
Function.prototype.method = function(name, func){
this.prototype[name] = func;
return this;
};
Number.method("testMethod",function(){
//return Math[ this < 0 ? "ceil" : "floor" ](this);
});
console.log(typeof (9.3).testMethod()); //also outputs undefined
案例4:
Function.prototype.method = function(name, func){
this.prototype[name] = func;
return this;
};
Number.method("testMethod",function(){
return Math[ this < 0 ? "ceil" : "floor" ](this);
});
console.log(typeof (9.3).testMethod()); //outputs number
我知道"返回此"将有助于链接添加的method()
。
在这些情况下,如果我要使用我添加到Function中的方法,return this
在Function.prototype.method
中的存在有什么不同呢?原型使用method()
。
下面是return
的代码:
Function.prototype.method = function(name,func){
this.prototype[name] = func;
return this;
}
function test(){
}
test.method('get',function(){
console.log('get method');
}).method('put',function(){
console.log('put method');
});
它工作得很好,但是如果你删除return
语句。它的结尾是:Uncaught TypeError: Cannot read property 'method' of undefined
相关文章:
- 得到"未定义不是函数“;使用显示原型图案时出错
- 如何将原型用于自定义方法和对象操作
- Javascript:继承原型而不重新定义构造函数
- AngularJS ng repeat显示原型函数未定义
- 在构造函数中定义函数会比将其附加到原型消耗更多的内存
- 为什么在原型中定义属性被视为反模式
- 使用原型对象向javascript对象添加自定义属性和方法的建议
- 可以'这'在原型函数中未定义
- JS原型无法设置属性'moveRight'的未定义
- 使用自定义原型实例化JavaScript函数
- 为什么不能使用lambda来定义原型函数
- 函数的自定义原型链
- javascript使用现有原型重新定义原型
- 仅为{..}对象定义原型
- 谷歌地图API -自定义原型标记图像从今天开始不显示
- 如何在定义原型函数时绑定它?
- 你能在JavaScript中使用自定义原型创建函数吗?
- 为XML原型函数的JavaScript定义原型属性
- 无法在 ES6 类定义中定义原型属性
- Javascript对象:重新定义原型是不行的