简体   繁体   English

如何在JavaScript中的函数内部访问变量?

[英]How to access a variable inside a function in javascript?

I am extremely new to javascript so apologize for questions that may be trivial ? 我对javascript非常陌生,因此对可能很琐碎的问题表示歉意吗?

var foo = function () {
    a = "10";
    this.b = "20"; 
};

foo.c = "30";

console.log(foo.a); // undefined
console.log(foo.c); // prints 30.

var foo1 = new foo();
console.log(foo1.b) // prints 20

How to access var "a" ? 如何访问var“ a”? Is it even possible ? 可能吗?

Actually, as long as you don't prefix the variable declaration with var it will be global: 实际上,只要不为变量声明加上var前缀,它将是全局的:

var foo = function () {
    a = "10";
    this.b = "20"; 
};

foo();

console.log(a); // is 10

Compared to: 相比:

var foo = function () {
    var a = "10"; // private scope
    this.b = "20"; 
};

foo();  

console.log(a); // is undefined

Using global variables: 使用全局变量:

var a = 10;
var foo = function() {
    a = 15;
    this.b = 20;
};
console.log(a); // equals 10
foo();
console.log(a); // equals 15

If you wanted to actually access the variable a within the function, much like your example: 如果您想实际访问函数中的变量a ,则非常类似于您的示例:

var foo = function() {
    a = 10; // private member
};

There is no way to access that private member. 无法访问该私人成员。

Turns out when you declare a variable in this way, you have created a global variable. 事实证明,当您以这种方式声明变量时,就创建了一个全局变量。 You can access it with console.log(a); 您可以使用console.log(a);访问它console.log(a); and it will return 10 . 它将返回10 However if you use var a = 10; 但是如果使用var a = 10; inside the function, you will not be able to access this member. 在该函数内部,您将无法访问该成员。

What you can do is return an object in your constructor that defines public members: 您可以做的是在构造函数中返回定义公共成员的对象:

function Foo() {
    var a = '10';

    function getA() {
        return a;
    }

    return {
        'a': a,
        'b': 20,
        'getA': getA
    };
}

var foo = new Foo();
console.log(foo.a) // 10
console.log(foo.b); // 20
console.log(foo.getA()); // 10

The code 编码

console.log(foo.a);
console.log(foo.c);

implies that you believe that a would be set as the equivalent of foo.a = "10" , that is setting a static field of the "class" foo . 暗示您相信a将被设置为foo.a = "10"的等效项,即设置“ class” foo的静态字段。 This is not the case in JavaScript. JavaScript中不是这种情况。

 a = "10";

This sets the global variable a to the value "10" , creating a global variable slot if necessary. 这会将全局变量a设置为值"10" ,并在必要时创建一个全局变量槽。 This type of global assignment should be avoided and is caught by tools like JSLint. 应该避免这种全局分配,并且会被诸如JSLint之类的工具捕获。

 this.b = "20"; 

This sets the field of the object passed as the this parameter of the function to "20" , creating the field slot if necessary. 这会将作为函数的this参数传递的对象的字段设置为"20" ,并在必要时创建字段槽。 Note that if you call foo directly, eg foo() , without new , the global scope is passed as this and this.b will also create a global variable. 请注意,如果直接调用foo (例如foo()而不使用new ,则将this方式传递全局范围,并且this.b还将创建一个全局变量。 This type of error can be caught by strict mode. 严格模式可以捕获此类错误。 If you execute in strict mode, undefined is passed in as the this parameter and this will throw an exception. 如果以严格模式执行,则将undefined作为this参数传入,这将引发异常。

var foo1 = new foo();

This creates a new, empty, object with the __proto__ property set to the value of the prototype property of foo and then calls foo passing the new object as the this parameter. 这将创建一个新的空对象,其__proto__属性设置为fooprototype属性的值,然后调用foo ,并将新对象作为this参数传递。 All fields of __proto__ appear as the default values of fields of the object. __proto__所有字段均显示为对象字段的默认值。 This leads to the convention of adding methods to the prototype object of the constructor function and initializing fields in the constructor function. 这导致了向构造函数的prototype对象添加方法以及初始化构造函数中的字段的约定。 Inheritance can be simulated by constructing an object with the ancestor prototype object as its __proto__ value and assigning it to the functions prototype property of the constructor function. 可以通过使用祖先原型对象作为其__proto__值构造一个对象并将其分配给构造函数的函数prototype属性来模拟继承。 The constructor function would also call the ancestor constructor function (without new ), passing its this as the constructor function's this parameter (using apply or call ). 构造函数也将调用祖先构造函数(不带new ),并将其this作为构造函数的this参数(使用applycall )传递。 This is all a bit awkward so ECMAScript 6 standardized this into a class construct which roughly translates to what I described. 这有点尴尬,因此ECMAScript 6将其标准化为一个class构造,该构造可以大致转换为我所描述的内容。

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

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