简体   繁体   English

在函数上返回 this

[英]returning this on function

can anybody tell me what's the point if any for a javascript function like this:任何人都可以告诉我这样的javascript函数有什么意义:

function f() { return this; } 

Note: I am trying to improve my javascript skills and found that looking at other people's code is very good.注意:我正在努力提高我的 javascript 技能,发现查看其他人的代码非常好。 I bumped into the above one and could not work out its point.我碰到了上面的那个,无法理解它的意思。

Inside an object, if you have在一个对象内部,如果你有

return this;

as the final line in a method, you can chain method calls one by one, like following syntax:作为方法的最后一行,您可以将方法调用一一链接,如下语法:

Foo.doSomething().doSomethingElse().doSomethingCompletelyDifferent();

As each methods return value, is the same object your working on.由于每个方法的返回值都是您处理的同一个对象。

Well, returning this from a standalone function is not much useful, I think it will be useful for you to know how the this value works:好吧,从独立函数返回this并没有多大用处,我认为了解this值的工作原理对您很有用:

The this value is implicitly set when: this值在以下情况下隐式设置:

  1. When a function is called as a method (the function is invoked as member of an object):当一个函数作为一个方法被调用时(该函数作为一个对象的成员被调用):

     obj.method(); // 'this' inside method will refer to obj
  2. A normal function call:一个普通的函数调用:

     myFunction(); // 'this' inside the function will refer to the Global object // or var global = (function () { return this; })();
  3. When the new operator is used:使用 new 运算符时:

     var obj = new MyObj(); // this will refer to a newly created object.

And you can also set the this keyword explicitly , with the call and apply methods:您还可以使用callapply方法显式设置 this 关键字:

function test () {
  alert(this);
}

test.call("Hello world");

As you can see, if you make a function call in no-object context (like example 2), the this value will refer to the Global object, which is not so much useful, unless you are looking for that.如您所见,如果您在无对象上下文中进行函数调用(如示例 2),则this值将引用 Global 对象,除非您正在寻找它,否则它没有多大用处。

When using function as methods, in object context returning this allows to build patterns of method chaining or fluent interfaces .当使用函数作为方法时,在对象上下文中返回 this 允许构建方法链流畅接口的模式

On constructor functions , this is the default return value, if you don't return any other object or you don't even have a return statement on your function, this will be returned.构造函数上this默认的返回值,如果你不返回任何其他对象,或者你的函数甚至没有return语句, this将被返回。

它用于返回函数本身,它在 jQuery 等库中大量使用,例如,用于启用函数链接,即:

var foo = function1().function2().function3();

If that is the entire contents of the function, it does indeed look pretty useless.如果这是函数的全部内容,它确实看起来很没用。 If that's just the tail end of the function, then as Doron says, it is useful for chaining.如果这只是函数的尾端,那么正如 Doron 所说,它对链接很有用。

I'd look at where it's called.我会看看它在哪里被调用。 It may be that it's some clever little trick.这可能是一些聪明的小把戏。

Usually when you see useless looking code -- like x=x+0 or if (flag) flag=true or that sort of thing -- it's a sign of either code that was edited carelessly, or there was a confused programmer.通常,当您看到看起来无用的代码时——比如x=x+0if (flag) flag=true或类似的东西——这表明代码被粗心编辑,或者有一个困惑的程序员。 But sometimes it really does do something strange but useful.但有时它确实做了一些奇怪但有用的事情。 The only way to know is to study the context.知道的唯一方法是研究上下文。 Of course, if your code is so obscure that others must study the context to figure it out, you probably should use clearer code or at least document it.当然,如果您的代码非常晦涩,以至于其他人必须研究上下文才能弄清楚,那么您可能应该使用更清晰的代码或至少记录下来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM