简体   繁体   English

变量对象中的函数声明

[英]Function declaration in variable object

test(); //working

function test(){//do something}

Is this related to (global)variable object? 这和(全局)变量对象有关吗? Does the function push to variable object before execution process start? 函数在执行过程开始之前是否将变量推送到变量对象?

UPDATE: Ok my real question is how "hoist" work. 更新:好的,我真正的问题是“提升”如何工作。 To understand you need to take a look at execution context of javascript ,and yes it related to variable object(which is part of execution context). 要理解,您需要查看javascript的执行上下文 ,是的,它与变量对象(属于执行上下文)有关。

See the Scope Cheatsheet on MDN 请参阅MDN上范围备忘单

function foo() { } hoists foo to the top of scope automatically function foo() { }自动将foo 提升到作用域的顶部

Is this related to (global)variable object? 这和(全局)变量对象有关吗? Does the function push to variable object before execution process start? 函数在执行过程开始之前是否将变量推送到变量对象?

Nope, it's just hoisting. 不,这只是吊装。


Consider this example: 考虑以下示例:

foo();
// ReferenceError: foo is not defined

(function(){
  foo();
  function foo (){
    console.log("A");
  };
})();
// A

(function(){
  foo();
  function foo (){
    console.log("B");
  };
})();
// B

foo();
// ReferenceError: foo is not defined

Hoisting just "lifts" the variable name to the top of the scope. 吊起只是将变量名称“提升”到作用域的顶部。


My opinion: 我的意见:

I never write functions as: 从不将函数编写为:

function foo() { ... }

I prefer being explicit because magic behavior sucks; 我更喜欢露骨,因为魔术行为很烂。 I always write functions as 总是写函数为

var foo = function() { ... }

If I feel like the function needs a name, I will give it a name, but still define with var 如果我觉得该函数需要一个名称,我会给它一个名称,但仍然使用var定义

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

@bfavaretto raises a valid concern: @bfavaretto引起了有效的关注:

About your opinion... var foo = function() { ... } also hoists foo to the top, but with an undefined value. 关于您的意见... var foo = function(){...}还将foo提升到顶部,但具有未定义的值。 So there's implicit stuff going on there too 所以那里也隐含着东西

True, it does hoist foo with an undefined value, but things will all work as desired. 是的,它确实使用undefined值来提升foo ,但是一切都会按需进行。

foo();
// TypeError: undefined is not a function
// This is good. We don't want the above function to run like that.
// This is a fatal error; the script won't even continue to run as-is.
// Comment out the above line to get it to run.

var foo = function foo() {
  console.log("foo");
};

var foo2 = function foo() {
  console.log("foo2");
};

// Even with the same function name, these two functions will
// output the correct values.
foo();  // foo
foo2(); // foo2

All of that said, it would be silly to have two functions with the same name identifier in the same scope. 综上所述,在同一个作用域中拥有两个具有相同名称标识符的功能是很愚蠢的。 But, even if you do make a mistake and write bad code like that, if you're using function expressions as described above, the code would still work anyway. 但是,即使您确实犯了一个错误并编写了类似的错误代码,但是,如果您如上所述使用函数表达式,该代码仍然可以正常工作。 If you just used named functions like in the original question, it certainly will not. 如果您只是像原始问题中那样使用命名函数,则肯定不会。

I think you are talking about Hoisting here. 我想您在这里谈论吊装 I always prefer labeled function instead of function expression. 我总是更喜欢标记函数而不是函数表达式。

this is a function expression: 这是一个函数表达式:

//Declaration 1
var foo = function(){

// code goes here
}
  • The func expression in this case is anonymous but assigned to a var foo. 在这种情况下,func表达式是匿名的,但已分配给var foo。 for reference 以供参考

This is a labeled function : 这是一个标记函数:

//Declaration 2
function foo(){
 // same code here
 }
  • There's not really any great reason to do expression to var. 没有真正的理由对var进行表达。 You should always try to use labeled statements for constructors,so you can identify an object's 'type' via its constructor. 您应该始终尝试对构造函数使用带标签的语句,以便可以通过其构造函数识别对象的“类型”。

  • people mostly use this where hoisting is needed. 人们通常在需要吊装的地方使用此工具。 Hoisting simply means calling something before it is defined . 吊装只是意味着在定义之前先打电话

    Very very simple example : 非常非常简单的示例

     foo(); // alerts 'hello' function foo() {alert('hello');} V/s foo(); // throws an error since foo is undefined var foo = function() {alert('hello');} 

REFER: var functionName = function() {} vs function functionName() {} 参考: var functionName = function(){}与function functionName(){}

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

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