var a = bc = function(){} 语法的用途

发布于 2024-12-27 14:58:15 字数 182 浏览 0 评论 0原文

我最近浏览了 js 代码,不断出现以下语法:

var foo = bar.bi = function() {...}

这对我来说是陌生的语法。难道只是为同一个函数定义两个名字吗?如果是这样,为什么不只将其定义为 bar.bi = function() 呢?

I've lately browsed js code and the following syntax keeps coming up:

var foo = bar.bi = function() {...}

This is unfamiliar syntax to me. Is it only to define two names for the same function? If so, why not only define it as bar.bi = function()?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仄言 2025-01-03 14:58:15

同时为变量和 bar 对象的 bi 属性分配相同的值。

通过这种方式,对象的属性可以获得值,但您仍然可以将其作为变量引用,这可能会更快一些。

实际上与...相同,

bar.bi = function() {...};
var foo = bar.bi;

foo === bar.bi; // true

或者您可以将其想象为...

var foo = ( bar.bi = function() {...} );

因此首先发生对 bar.bi 的赋值。从赋值表达式返回的结果是相同的函数,并且该结果被赋值给foo

Assigns the same value to the variable and the bi property of the bar object at the same time.

This way the object's property gets the value, but you can still reference it as a variable, which is likely a little faster.

Effectively the same as...

bar.bi = function() {...};
var foo = bar.bi;

foo === bar.bi; // true

Or you can visualize it as...

var foo = ( bar.bi = function() {...} );

So the assignment to bar.bi happens first. The result returned from the assignment expression is the same function, and that result is assigned to foo.

酒绊 2025-01-03 14:58:15

除了将函数分配给 2 个变量之外,上下文也会根据您调用它的方式而变化。

bar.bi();

会将其上下文作为 bar 对象,就好像您会使用它一样:

foo.call(bar);

但在另一个变量中使用它,如下所示:

foo();

将使用 foo 的上下文。因此,如果 foo 位于全局上下文中,它将相当于:

bar.bi.call(window);

In addition to assigning the function to 2 variable, the context also changes depending on how you call it.

bar.bi();

would have it's context as the bar object, as if you would have used this:

foo.call(bar);

But using it off the other variable, like this:

foo();

would use the context of foo. So if foo is in the global context, it'll be equivalent to this:

bar.bi.call(window);
南渊 2025-01-03 14:58:15

这只是一个复合赋值,

var r = x = 3;

将 3 赋给 x,也赋给新声明的 r。

您的示例只是用一个函数代替 3,用一个对象属性 - bar.bi - 代替 x

It's just a compound assignment

var r = x = 3;

assigns 3 to x, and also to r, which is newly declared.

Your example just substitutes a function in place of 3, and an object property—bar.bi—in place of x.

鹿港小镇 2025-01-03 14:58:15

这取决于它的使用地点。

foobar.bi 都指向相同的函数。 调用该函数

foo();

这意味着可以使用and

bar.bi();

,同时函数内部 this 的值有所不同。要使第一个等于第二个,应按如下所示调用它

foo.call(bar);  

foo.apply(bar);

这可确保 this 将指向函数内的 bar

请参考:

https://developer.mozilla.org/en/JavaScript/参考/Global_Objects/函数/调用

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects /函数/应用

It depends on where it is used.

Both fooand bar.bi point to same function here. That means the function can be invoked using

foo();

and

bar.bi();

At the same time it differs in the value of this inside the function. To make the first one equal to second one, it should be invoked as given below

foo.call(bar);  

or

foo.apply(bar);

This ensures that this will point to bar inside the function.

please refer:

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/call.

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/apply.

.

她如夕阳 2025-01-03 14:58:15
var foo = bar.bi = function() {...};



bar.bi === function() {...} //true

foo === bar.bi //true

bar 将是一个响应方法 bi 的对象。

var foo = bar.bi = function() {...};



bar.bi === function() {...} //true

foo === bar.bi //true

bar will be an object who responds to method bi.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文