栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将变量设置为等于没有括号的函数?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将变量设置为等于没有括号的函数?

他们在那里所做的是在不 调用 函数的情况下 引用 该函数。 __

var x = foo;   // Assign the function foo to xvar y = foo(); // Call foo and assign its *return value* to y

在Javascript中,函数是对象。正确的对象。这样您就可以传递对它们的引用。

在这种特定情况下,他们正在做的是设置

handleServerResponse
为就绪状态更改时XHR对象使用的回调。XHR对象将在执行ajax请求的过程中调用该函数。

其他示例:

// Declare a functionfunction foo() {    console.log("Hi there");}// Call itfoo();        // Shows "Hi there" in the console// Assign that function to a variblevar x = foo;// Call it againx();          // Shows "Hi there" in the console// Declare another functionfunction bar(arg) {    arg();}// Pass `foo` into `bar` as an argumentbar(foo);     // Shows "Hi there" in the console, because `bar`   // calls `arg`, which is `foo`

它自然地遵循从一个事实,即函数对象,但它的价值呼唤特别是有之间没有神奇的联系

x
,并
foo
在上面;
它们都是指向相同功能的变量。除了它们指向同一功能外,它们没有任何链接,并且更改一个功能(例如指向另一个功能)对另一个功能没有影响。例:

var f = function() {    console.log("a");};f(); // "a"var x = f;x(); // "a"f = function() {    console.log("b");};f(); // "b"x(); // "a" (changing `f` had no effect on `x`)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/485352.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号