这在 Javascript 中意味着什么?
What does this mean in Javascript?
我注意到以下代码:
> (123[45] = 67) == 67
<- true
> 123[45]
<- undefined
您也可以在浏览器中尝试。
我不明白这是怎么回事。
123[45]
在第一个赋值指令中被视为一个数组,实际上正确地响应下一个测试 == 67
。
但是,当我尝试访问内存位置时 123[45]
我得到了 undefined.
那是什么?
Primitive values cannot have properties (that's what distinguishes them from objects). See also Strings are not object then why do they have properties?, Why can't I add properties to a string object in javascript? and 关于那个主题。
It actually responds rightfully to the next test == 67
.
不是 属性 访问权限,不。只是无论赋值目标发生什么情况,赋值表达式总是求值为它的右边值。
您也可以尝试使用实际对象:
var x = {
get p() { console.log("getting"); return 42; },
set p(val) { console.log("setting "+val); }
};
x.p = 2; // setting 2
console.log(x.p); // getting 42
console.log((x.p = 67) == 67); // setting 67 true - no "getting"!
console.log(x.p); // still: getting 42
我注意到以下代码:
> (123[45] = 67) == 67
<- true
> 123[45]
<- undefined
您也可以在浏览器中尝试。
我不明白这是怎么回事。
123[45]
在第一个赋值指令中被视为一个数组,实际上正确地响应下一个测试 == 67
。
但是,当我尝试访问内存位置时 123[45]
我得到了 undefined.
那是什么?
Primitive values cannot have properties (that's what distinguishes them from objects). See also Strings are not object then why do they have properties?, Why can't I add properties to a string object in javascript? and
It actually responds rightfully to the next test
== 67
.
不是 属性 访问权限,不。只是无论赋值目标发生什么情况,赋值表达式总是求值为它的右边值。
您也可以尝试使用实际对象:
var x = {
get p() { console.log("getting"); return 42; },
set p(val) { console.log("setting "+val); }
};
x.p = 2; // setting 2
console.log(x.p); // getting 42
console.log((x.p = 67) == 67); // setting 67 true - no "getting"!
console.log(x.p); // still: getting 42