(node v4.2.4)
その1
'use strict'; let arr = [{id: 1}, {id: 2}, {id: 3}]; for (let x in arr) { console.log(x); } console.log('----'); arr.foo = "hello"; console.log(arr); console.log('----'); for (let x in arr) { console.log(x); } console.log('----'); for (let x = 0; x < arr.length; x++) { console.log(arr[x]); } console.log('----'); arr.forEach(function(x) { console.log(x); }); console.log('----'); for (let x of arr) { console.log(x); }
実行結果
0
1
2
----
[ { id: 1 }, { id: 2 }, { id: 3 }, foo: 'hello' ]
----
0
1
2
foo
----
{ id: 1 }
{ id: 2 }
{ id: 3 }
----
{ id: 1 }
{ id: 2 }
{ id: 3 }
----
{ id: 1 }
{ id: 2 }
{ id: 3 }
その1の2 let arr = [ 3, 5, 7 ]; の場合の実行結果
0 1 2 ---- [ 3, 5, 7, foo: 'hello' ] ---- 0 1 2 foo ---- 3 5 7 ---- 3 5 7 ---- 3 5 7
その1の3 let arr = {a: 1, b:2, c: 3}; の場合の実行結果
a
b
c
----
{ a: 1, b: 2, c: 3, foo: 'hello' }
----
a
b
c
foo
----
----
TypeError
----
TypeError
その2
'use strict'; var obj1; var prop1; var obj2; var prop2; obj1 = {a:1, b:2, c:3}; for (prop1 in obj1) { console.log("obj1." + prop1 + " = " + obj1[prop1]); } console.log('----'); function Hoge() { this.color = "red"; } Hoge.prototype = obj1; obj2 = new Hoge(); for (prop2 in obj2) { console.log("obj2." + prop2 + " = " + obj2[prop2]); } console.log('----'); for (prop2 in obj2) { if( obj2.hasOwnProperty( prop2 ) ) { console.log("obj2." + prop2 + " = " + obj2[prop2]); } }
実行結果
obj1.a = 1 obj1.b = 2 obj1.c = 3 ---- obj2.color = red obj2.a = 1 obj2.b = 2 obj2.c = 3 ---- obj2.color = red
そのほか
そのうち追記するかも