简体   繁体   English

关于JavaScript中的函数变量

[英]Regarding function variables in javascript

I am new to javascript so please understand if the question is a bit naive. 我是javascript新手,所以请理解问题是否太幼稚。 I have heard that functions are also objects in javascript . 我听说函数也是javascript中的对象。 So that means functions can also have properties like objects. 因此,函数也可以具有对象等属性。 So I tried this : 所以我尝试了这个:

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

The output of this is undefined . 此输出undefined I dont understand what is happening. 我不明白发生了什么。 So when I declare the variable v1 in the function foo ,according to the result v1 is not a property of the function-object foo .If it is not the former then what is it a property of ? 因此,当我在foo函数中声明变量v1时,根据结果v1不是函数对象foo的属性。如果不是前者,那么它的属性是什么? Could some one explain to me what is happening ? 有人可以告诉我发生了什么吗?

You're right in Javascript function is a object. 您对Javascript函数中的对象是正确的。 You can have attribute in a function object, examples are link length etc. 您可以在函数对象中具有属性,例如链接长度等。

according to the result v1 is not a property of the function-object foo. 根据结果​​v1不是功能对象foo的属性。

v1 is just a variable defined in function foo, it is not a attribute. v1只是在函数foo中定义的变量,它不是属性。

To add a attribute you could use foo.v1 = "1" to add attribute v1 to object foo. 要添加属性,可以使用foo.v1 = "1"将属性v1添加到对象foo。

If you use console.log(v1) instead of console.log(foo.v1). 如果您使用console.log(v1)而不是console.log(foo.v1)。 You'll see output 1. Here you're accessing a local variable inside a function. 您将看到输出1。在这里,您正在访问函数内部的局部变量。

You might think var foo is already a object why can't I access it inside the function? 您可能会认为var foo已经是一个对象,为什么我不能在函数内部访问它? This because these two foo variables are in different scopes. 这是因为这两个foo变量的作用域不同。 You might want to learn more about function scope 您可能想了解有关功能范围的更多信息

Object properties and variables are not the same thing. 对象的属性和变量不是同一回事。 Functions can have both local variables (this includes function arguments) and properties: 函数可以具有局部变量(包括函数参数)和属性:

function foo () {
    v1 = 1; // this is a local variable
    return [
        v1,    // local var above
        foo.v2 // object property
    ]
}

foo(); // returns [1,undefined]

foo.v2 = 2;
foo(); // returns [1,2]

So you see, functions are indeed objects. 如此看来,功能确实是对象。 But variables inside the function have nothing to do about the fact that functions are objects. 但是函数内部的变量与函数是对象这一事实无关。

Side note: the fact that functions are first-class objects and that functions can be defined anonymously (without a name) are two different features. 附带说明:函数是一类对象,并且可以匿名定义(没有名称)函数是两个不同的特性。 In C for example, functions are also first-class objects - that is, you can assign them to function pointers. 例如,在C语言中,函数也是一流的对象-也就是说,您可以将它们分配给函数指针。 But in C you can't declare functions without a name. 但是在C语言中,您不能在没有名称的情况下声明函数。

function bar() {}

b = bar; // this demonstrates that functions are first-class objects
c = function(){}; // this demonstrates that functions can be anonymous

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

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