为什么以这种方式交换字符有效 JavaScript?
Why is swapping characters this way valid JavaScript?
我对这段代码的工作原理感到困惑。我假设 A
需要 'A'
才能交换这两个字符,但它有效。另外,这有什么作用的名称吗?从它的外观来看,我认为它是解构的,但我不确定。
var translations = {
A : 'U'
};
console.log(translations['A']); //returns U
我本以为你需要这样写:
var translations = {
'A' : 'U'
};
console.log(translations['A']); //also returns U
对象或属性键可以是标识符名称(即标识符+保留字)、字符串文字或数字文字。在访问它的方式中将它称为 A
还是 'A'
并不重要。 https://ecma-international.org/ecma-262/6.0/#sec-object-initializer
属性 名字
Property names must be strings. This means that non-string objects
cannot be used as keys in the object. Any non-string object, including
a number, is typecasted into a string via the toString method.
var object = {};
object['1'] = 'value';
console.log(object[1]);
This outputs "value", since 1 is type-casted into '1'.
var foo = {unique_prop: 1}, bar = {unique_prop: 2}, object = {};
object[foo] = 'value';
console.log(object[bar]);
This also outputs "value", since both foo and bar are converted to the
same string. In the SpiderMonkey JavaScript engine, this string would
be "['object Object']".
此外,您所做的基本上是创建一个 Object。我没有看到你在任何地方摧毁它。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer
创建对象
However, the advantage of the literal or initializer notation is, that
you are able to quickly create objects with properties inside the
curly braces. You simply notate a list of key: value pairs delimited
by comma. The following code creates an object with three properties
and the keys are "foo", "age" and "baz". The values of these keys are
a string "bar", a number 42, and another object.
var object = {
foo: 'bar',
age: 42,
baz: {myProp: 12}
}
访问属性
Once you have created an object, you might want to read or change
them. Object properties can be accessed by using the dot notation or
the bracket notation. See property accessors for detailed information.
object.foo; // "bar"
object['age']; // 42
object.foo = 'baz';
One can think of an object as an associative array (a.k.a. map,
dictionary, hash, lookup table). The keys in this array are the names
of the object's properties. It's typical when speaking of an object's
properties to make a distinction between properties and methods.
However, the property/method distinction is little more than a
convention. A method is simply a property that can be called, for
example if it has a reference to a Function instance as its value.
我对这段代码的工作原理感到困惑。我假设 A
需要 'A'
才能交换这两个字符,但它有效。另外,这有什么作用的名称吗?从它的外观来看,我认为它是解构的,但我不确定。
var translations = {
A : 'U'
};
console.log(translations['A']); //returns U
我本以为你需要这样写:
var translations = {
'A' : 'U'
};
console.log(translations['A']); //also returns U
对象或属性键可以是标识符名称(即标识符+保留字)、字符串文字或数字文字。在访问它的方式中将它称为 A
还是 'A'
并不重要。 https://ecma-international.org/ecma-262/6.0/#sec-object-initializer
属性 名字
Property names must be strings. This means that non-string objects cannot be used as keys in the object. Any non-string object, including a number, is typecasted into a string via the toString method.
var object = {};
object['1'] = 'value';
console.log(object[1]);
This outputs "value", since 1 is type-casted into '1'.
var foo = {unique_prop: 1}, bar = {unique_prop: 2}, object = {};
object[foo] = 'value';
console.log(object[bar]);
This also outputs "value", since both foo and bar are converted to the same string. In the SpiderMonkey JavaScript engine, this string would be "['object Object']".
此外,您所做的基本上是创建一个 Object。我没有看到你在任何地方摧毁它。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer
创建对象
However, the advantage of the literal or initializer notation is, that you are able to quickly create objects with properties inside the curly braces. You simply notate a list of key: value pairs delimited by comma. The following code creates an object with three properties and the keys are "foo", "age" and "baz". The values of these keys are a string "bar", a number 42, and another object.
var object = {
foo: 'bar',
age: 42,
baz: {myProp: 12}
}
访问属性
Once you have created an object, you might want to read or change them. Object properties can be accessed by using the dot notation or the bracket notation. See property accessors for detailed information.
object.foo; // "bar"
object['age']; // 42
object.foo = 'baz';
One can think of an object as an associative array (a.k.a. map, dictionary, hash, lookup table). The keys in this array are the names of the object's properties. It's typical when speaking of an object's properties to make a distinction between properties and methods. However, the property/method distinction is little more than a convention. A method is simply a property that can be called, for example if it has a reference to a Function instance as its value.