简体   繁体   English

对javascript函数表达式感到困惑

[英]Confused about javascript function expression

I just started to learn about javascript etc, and I get a project from my friend to take a look at it, and what I saw there is that they are using javascript functions all the time as a function expression, like this: 我刚刚开始学习javascript等,我从朋友那里得到一个项目来研究它,我看到的是他们一直在使用javascript函数作为函数表达式,如下所示:

var a = function(Id) {
    //Do something here
}

I could say like ok, they'r working like that and I'm gonna work like that also like robot, even if I don't understand why is that like that.. 我可以说很好,他们像那样工作,我也要像机器人一样那样工作,即使我不明白为什么会那样。

So I'm wondering what is this acctually, (in that code I posted above) is that function called "a" which is expecting 1 argument called "Id" in our case so I could call it on click of some button for example, or maybe this var a is acctually variable which is called " a " but it might be called as a function and acctually it is a javascript function? 所以我想知道这到底是什么(在我上面发布的代码中)是一个名为“ a”的函数,在我们的例子中,该函数期望有一个名为“ Id”的参数,因此我可以在单击某些按钮时调用它,还是这个var a可能是准确的变量,称为“ a ”,但它可能被称为一个函数,并且准确地说是javascript函数?

This is little bit confusing me here, and I saw in this project that almost all javascrit functions are used like this, but is this a right approach or they should use named functions expression so it might look like this: 这让我有些困惑,在这个项目中我看到几乎所有的javascrit函数都是这样使用的,但这是正确的方法,还是它们应该使用命名函数表达式,所以看起来像这样:

var a = function myFunction(Id) {
    //Do something here
}

So what is this now ? 那现在是什么? I created function called myFunction which is expecting 1 argument called "Id" and it could be called on click by myFunction and also by calling "a" varible ? 我创建了一个名为myFunction的函数,该函数需要1个称为“ Id”的参数,并且可以在单击时通过myFunction调用,也可以通过调用“ a”变量来调用? (if it is variable, or it is also function name or whatever).. (如果它是变量,或者它也是函数名或其他名称)。

Thanks guys Cheers 谢谢大家的欢呼

The key to understanding this is that, in JavaScript, functions are data, just like any other. 理解这一点的关键是,在JavaScript中,函数就是数据,就像其他任何函数一样。 So, if you are ok understanding this: 因此,如果您可以理解以下内容:

var x = 10;

If you were to try to get the value of x , you'd get 10 : 如果尝试获取x的值,则将得到10

console.log(x);

Then, understanding this isn't that much of a stretch: 然后,了解这一点并不是一件容易的事:

var x = function(){
   // do something
}

if you were to try to get the value here, you'd get the actual text of the function: 如果要尝试在此处获取值,则将获取该函数的实际文本:

console.log(x); // function(){ // do something }

Since this last version retrieves a function, you could then invoke the function by simply appending () to the returned value, like this: 由于此最新版本检索了一个函数,因此您可以通过将()附加到返回值上来调用该函数,如下所示:

x(); // This would invoke the function stored in x

The alternative approach is with a "function declaration" which looks like this: 另一种方法是使用“函数声明”,如下所示:

function x(){
  // Do something
}

Here, there is no variable storing the function... x is the function and to invoke it, you'd do it directly: 在这里,没有变量存储函数... x是函数,要调用它,您可以直接执行:

x();

However, in JavaScript, all declarations are "hoisted" to the top of their enclosing scope, so this: 但是,在JavaScript中,所有声明都“悬挂”在其封闭范围的顶部,因此:

var x = function(){};

would cause the declaration of x to be hoisted, but not the value of x , while this: 会导致x的声明被悬挂,而不是x的值,而这是:

function x(){ ...};

would cause the entire declaration to be hoisted. 将导致整个声明被吊起。

The difference is that a declaration would allow you to invoke the function in code the precedes the actual declaration (since hoisting would cause the function to be read first) and with an expression, if you tried this, you would get an error stating that x is not a function . 区别在于,声明使您可以在代码中调用该函数,该代码先于实际声明(因为提升将导致首先读取该函数),并且带有表达式,如果您尝试这样做,则会得到一条错误消息,指出x is not a function

Here's an example of this: 这是一个例子:

 // Print the return value from runing the function who's name is "x". // This will succeed even though we haven't even declared the function x yet becuase all // declarations are hoisted to the top of their enclosing scope; console.log(x()); // Print the return value from runing the function stored in the variable called "y": // This will fail with an error stating that y is not a function because only the variable // declaration (var y) is hoisted to the top of the enclosing scope, but not the value assigned to it. console.log(y()); // This is a function "declaration" for a function who's name is "x" function x(){ return "hello from function declaration x"; } // This is a function expression being assigned as the data for a variable var y = function(){ return "hello from function expression y"; } 

Both are useful and often interchangeable, but hoisting causes differences in how (and when) you can call the function. 两者都很有用,而且通常可以互换,但是吊装会导致调用函数的方式(和时间)有所不同。

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

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