習慣沉默2017-07-05 11:09:29
var o = {
name: 'foo',
hey: 'bar'
}
console.log(o.name);
// => "foo"
console.log(o['name']);
// => "foo"
上述普通的js物件只能用字串做key,es6有個新特性,可以讓「值」當key 看範例:
var m = new Map();
var eczn = {
name: 'eczn',
age: 20
}
m.set(eczn, 'map Obj to Stirng');
console.log(m);
黄舟2017-07-05 11:09:29
javascript中的陣列不支援用數字以外的標識符做為數組的下標,但是你可以用物件來實現和php中關聯數組相似的效果:
var myArray = {'key1': 'value1'};
console.log(myArray['key1']); // 会输出value1, 其实相当于myArray.key1
曾经蜡笔没有小新2017-07-05 11:09:29
可以的,比如說
var person = {
"name" : "孤月"
};
var n = "name";
console.log(person[n]);
//设置键名
person[n] = "deep dark fantasy";