简体   繁体   English

在 JavaScript 中调用函数对象

[英]Invoking a function Object in JavaScript

I have a small question in JavaScript.我在 JavaScript 中有一个小问题。

Here is a declaration:这是一个声明:

function answerToLifeUniverseAndEverything() {
   return 42;
}

var myLife = answerToLifeUniverseAndEverything();

If I do console.log(myLife) , it will print 42 , as I am just invoking the same instance of function resulting in 42 as the answer.如果我执行console.log(myLife) ,它将打印42 ,因为我只是调用相同的函数实例,结果42作为答案。 (Basic rule on JavaScript that only references of objects are passed and not the object.) (JavaScript 的基本规则是只传递对象的引用而不传递对象。)

Now, on the other, hand if I do:现在,另一方面,如果我这样做:

var myLife = new answerToLifeUniverseAndEverything();

then I can't invoke the function;那么我无法调用该函数; instead, myLife becomes just an object?相反, myLife变成了一个对象? I understand that this is a new copy of the same function object and not a reference, but why can't I invoke the method?我知道这是同一个函数对象的新副本而不是引用,但为什么我不能调用该方法?

Can you please clarify the basic fundamental I am missing here?你能澄清我在这里缺少的基本原理吗?

By prefixing the call to answerToLifeUniverseAndEverything() with new you are telling JavaScript to invoke the function as a constructor function , similar (internally) to this:通过给answerToLifeUniverseAndEverything()的调用加上new前缀,你是在告诉 JavaScript 将该函数作为构造函数调用,类似于(内部)这样的:

var newInstance = {};
var obj = answerToLifeUniverseAndEverything.call(newInstance); // returs 42
if (typeof obj === 'object') {
  return obj
} else {
  return newInstance;
}

JavaScript proceeds to initialize the this variable inside the constructor function to point to a new instance of answerToLifeUniverseAndEverything . JavaScript 继续在构造函数中初始化this变量以指向answerToLifeUniverseAndEverything的新实例。 Unless you return a different Object yourself, this new instance will get returned, whether you like it or not.除非你自己返回一个不同的Object ,否则这个新实例被返回,无论你喜欢与否。

When you do var myLife = answerToLifeUniverseAndEverything();当你做var myLife = answerToLifeUniverseAndEverything(); , myLife is simply holding the return value from the function call - in this case, 42. myLife knows nothing about your function in that case, because the function was already called, returned, and then it assigned the resulting value (42) to the new variable myLife . ,MYLIFE只是从功能呼叫保持的返回值-在这种情况下,42 myLife一无所知在这种情况下你的函数,因为该函数已经调用,返回,然后将其分配结果值(42)至新变量myLife

A completely different thing happens when you do var myLife = new answerToLifeUniverseAndEverything();当您执行var myLife = new answerToLifeUniverseAndEverything();时,会发生完全不同的事情var myLife = new answerToLifeUniverseAndEverything(); - instead, a new object is created, passed to the function as this , and then (assuming the function doesn't return an object itself), stored in the newly created variable. - 相反,创建一个新对象,将其作为this传递给函数,然后(假设函数本身不返回对象)存储在新创建的变量中。 Since your function returns a number, not an object, the newly generated object is stored.由于您的函数返回的是数字,而不是对象,因此存储了新生成的对象。

Try:尝试:

function answerToLifeUniverseAndEverything() {
  return 42;
}

var myLife = answerToLifeUniverseAndEverything;

alert(myLife());

When you do:当你这样做时:

var myLife = answerToLifeUniverseAndEverything();

you're assigning the function result to myLife ie 42.您正在将函数结果分配给myLife即 42。

I think i've described the behaviour of new elsewhere.我想我已经在别处描述了new的行为。 Basically when you do new f() the JS engine creates an object and passes that as this , then uses that object if the return value of f() is not an object.基本上,当您执行new f() ,JS 引擎会创建一个对象并将其作为this传递,然后如果f()的返回值不是对象,则使用该对象。

eg.例如。

o = new f();

is equivalent (approximately) to相当于(大约)

temp = {};
temp2 = f.call(temp);
o = typeof temp2 === "object" ? temp2 : temp;

If I do console.log(myLife) It'll print 42, as I am just invoking the same instance of function resulting in 42 as the answer.如果我执行console.log(myLife)它将打印 42,因为我只是调用相同的函数实例,结果为 42 作为答案。 (Basic rule on javascripts that only references of objects are passed and not the object) (javascripts 的基本规则是只传递对象的引用而不传递对象)

Not quite.不完全是。 This is because you're assigning the return value of answerToLifeUniverseAndEverything() to myLife .这是因为您将answerToLifeUniverseAndEverything()的返回值分配给myLife If you wanted to make a copy of the function, drop the brackets:如果您想复制该函数,请去掉括号:

var myLife = answerToLifeUniverseAndEverything;
console.log(myLife());

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

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