扩充基本类型(来自JavaScript:好的部分)-为什么';这';返回?

扩充基本类型(来自JavaScript:好的部分)-为什么';这';返回?,javascript,this,Javascript,This,在第33页,作者展示了在向基本类型添加新方法时避免键入“prototype”的技巧,随后是一个提取数字整数部分的示例 Function.prototype.method = function (name, func) { this.prototype[name] = func; return this; // removal of this line still yields the same result }; Number.method('integer', functio

在第33页,作者展示了在向基本类型添加新方法时避免键入“prototype”的技巧,随后是一个提取数字整数部分的示例

Function.prototype.method = function (name, func) {
    this.prototype[name] = func;
    return this; // removal of this line still yields the same result
};

Number.method('integer', function () {
    return Math[this < 0 ? 'ceil' : 'floor'](this);
});

console.log((-10 / 3).integer());
Function.prototype.method=函数(名称,func){
this.prototype[name]=func;
返回此;//删除此行仍然会产生相同的结果
};
Number.method('integer',function(){
返回数学[this<0?'ceil':'floor'](this);
});
console.log(-10/3.integer());

输出如预期的那样为-3,即使我注释掉返回this的行。这行完成了什么?

当您调用
Number.method(…)
时,它返回原型对象,这将允许您链接它:
Number.method(…).method(…)


由于您的代码在调用
方法
方法时忽略了返回值,因此更改该返回值没有任何效果。

可能是这样,因此您可以链接
.method(/*…*/).method(/*…*/)
,但我很困惑,为什么需要这样做呢,虽然它试图让生活变得更轻松,但与仅仅使用书中没有的工具相比,它又慢又笨重)。