简体   繁体   English

关于JavaScript变量的快速问题

[英]quick question about JavaScript variables

Not sure what to search for here, so apologies if I'm repeating another question. 不知道在这里搜索什么,如果我重复另一个问题,请道歉。

I'm wondering if there are any issues that I'm not aware of with using the following syntax in JavaScript: 我想知道在JavaScript中使用以下语法是否存在任何我不知道的问题:

var a = {};

var b = a.niceCoat = {};

Seems handy, but I just want to make sure... 看起来很方便,但我只是想确保......

That is perfectly fine, because a was declared previously. 这是完全正常的,因为a以前声明。 The expressions will be evaluated as 表达式将被评估为

var a = {};
var b = (a.niceCoat = {});

Ie it first assigns a new empty object to a.niceCoat and the result (the result of an assignment is the assigned value) to b . 也就是说,它首先将一个新的空对象分配给a.niceCoat ,并将结果(赋值的结果是指定的值)分配给b

But be aware of something like 但要注意类似的东西

var a = b = 'c';

which, again, is evaluated as 再次,评估为

var a = (b = 'c');

Only a will be in local scope, b would be global . 只有a将在本地范围内, b将是全局的 If you want b to be local too, you have to declare it beforehand: var b; 如果你想让b也是本地的,你必须事先声明它: var b; . Something like var a = var b = .... does not work (not valid syntax). 喜欢的东西var a = var b = .... 不起作用 (无效语法)。


Slightly off topic: 稍微离题:

This method is indeed handy. 这种方法确实很方便。 Imaging you have an object of objects, something like: 成像你有一个物体的对象,如:

var map = {
    foo: {},
    bar: {}
};

and you want to get the object for a certain key or create a new one if the key does not exists. 并且您希望获取某个键的对象,或者如果该键不存在则创建一个新键。 Normally one would probably do: 通常人们可能会这样做:

var obj = map[key];
if(!obj) { // works because if the key is set, it is an object 
    obj = {}; //                                        which evals to true
    map[key] = obj;
}
// now work with obj

With the above method, this can be shortened to 通过上述方法,可以缩短到这一点

var obj = map[key];
if(!obj) {
    map[key] = obj = {};
}

And we can make it even shorter with the logical OR operator ( || ) : 我们可以使用逻辑OR运算符( ||使其更短:

var obj = map[key] || (map[key] = {});

(though it might be less readable). (虽然它可能不太可读)。

You can do that. 你可以做到这一点。 a.niceCoat = {} will be evaluated first, which assigns the object to the property and also has the object as result, which you then can assign to the variable. 将首先评估a.niceCoat = {} ,它将对象分配给属性,并将对象作为结果,然后您可以将该对象分配给变量。

You should however be aware that b and a.niceCoat are now referencing the same object, so if you put anything in the object it will show up for both the variable and the property: 但是您应该知道ba.niceCoat现在引用相同的对象,因此如果您在对象中放入任何内容,它将显示变量和属性:

var a = {};
var b = a.niceCoat = {};

b.x = 42;
alert(a.niceCoat.x); // shows 42

There no issue with that. 这没有问题。 It's the same as: 它与以下相同:

var b = (a.niceCoat = {});

Which is the same as: 这与以下相同:

a.niceCoat = {};
var b = a.niceCoat; // Now b and a.niceCoat are the same object

Just be careful with declaring entirely new variables with it like: 只需注意用它声明全新的变量,如:

var i = j = 0;

Which is the same as: 这与以下相同:

j = 0;
var i = j;

Notice how j is not declared with the var keyword. 请注意如何使用var关键字声明j。

this is how you create an empty object in javascript. 这是你在javascript中创建一个空对象的方法。 nothing wrong with it. 这没什么不对。

Yep, absolutely valid. 是的,绝对有效。

Eg 例如

var a = {};

var b = a.niceCoat = {};

a.foo = function() { alert('foo!'); };


a.foo(); // shows the alert

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

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