如下,关于javascript
中数组的自定义属性,应该怎么理解
var ary = ["a", "b", "c"];
ary.name = "tom";
ary.age = 3;
console.log(ary); // ["a", "b", "c", name: "test"]
console.log(ary.length); // 3
console.log(Array.isArray(ary)); // true
console.log(ary.name); // "tom"
console.log(ary.age); // 3
ary.forEach(function (v) {
console.log(v); // 依次打印 a b c
});
for (var prop in ary) {
console.log(ary[prop]); // 依次打印 tom 3
}
相关分类