【发布时间】:2011-11-28 18:59:11
【问题描述】:
我想知道,“返回这个”在 javascript 函数中做了什么,它的目的是什么? 假设我们有以下代码:
Function.prototype.method = function (name, func) {
this.prototype[name] = func;
return this;
};
“return this”在函数内部做了什么?
我知道上面的代码做了什么,以及“this”关键字的用途。我只是不知道“返回这个”在函数内部做了什么。
【问题讨论】:
-
@user722756:因为
method被添加到Function.prototype,this将引用调用method的函数。该函数可能被用作“构造函数”,因为method正在扩展该函数的prototype对象。 -
我知道“this”关键字的用法我只是不知道函数内部“return this”的用法。
-
return this用于创建fluent interface。请参阅下面@marcioAlmada 和@AdamRackis 发布的答案。 -
我猜你没有理解我的问题。我知道上面的代码是做什么的,我只是不知道“return this”是做什么的。
标签: javascript