var a= function (){var bb = 12; this.aa ="xxx"}; a.aa="www"; a.prototype.cc="eee"; var b = new a; for (var i in b){ console.log(b[i]); } console.log (b) console.log(b.aa) console.log(b.bb)
xxx
eee
a {aa: "xxx", cc: "eee"}
xxx
undefined
总结,构造函数,传递给实例的是this和prototype定义,其他自身的定义的变量,没用传递给实例
var a= function (){var bb = 12; this.aa ="xxx"}; a.aa="www"; a.prototype = "eee";//1 或者“ss” new Number(1111) function(){this.cc="ddd"};
;
如果将prototype 不赋值为object,而是赋值为原始值,prototype不会传递给实例
xxx
a {aa: "xxx"}
xxx
undefined
var a= function (){var bb = 12; this.aa ="xxx"}; a.aa="www"; a.prototype = new Number(1111); var b = new a; for (var i in b){ console.log(b[i]); } console.log (b) console.log(b.aa) console.log(b.bb)
xxx
a {aa: "xxx"}
xxx
undefined
var a= function (){var bb = 12; this.aa ="xxx"}; a.aa="www"; a.prototype = new String("11111"); var b = new a; for (var i in b){ console.log(b[i]); } console.log (b) console.log(b.aa) console.log(b.bb)
xxx
5undefined
a {aa: "xxx", 0: "1", 1: "1", 2: "1", 3: "1", 4: "1"}
xxx
undefined
new String,相当于使用数组对象存储的,但是遍历不出来值。
var a= function (){var bb = 12; this.aa ="xxx"}; a.aa="www"; a.prototype = [1,2]; var b = new a; for (var i in b){ console.log(b[i]); } console.log (b) console.log(b.aa) console.log(b.bb)
xxx
1
2
[aa: "xxx", 0: 1, 1: 2]
xxx
undefined
prototype就是{},实例化的对象,key value形式的,其他的都不行,不做传递。
var a=[1,2,3]; for(var i in a){ console.log(typeof i); console.log(a[i]); }
string
1
string
2
string
3
数组的下标和对象一样,都是stirng
var a=[1,2,3]; for(var i in a){ console.log(typeof i); console.log(a[i]); } var b={0:a,1:b} console.log(b[0]) console.log(b[1])
string
1
string
2
string
3
[1, 2, 3]
undefined
这就是数组的基本方式,
在对象中,如果是数组方式当key,不能使用b.1或者b."1",会报语法错误,跟array保持一致了
b[0]===b["0"] 为ture
总结:
只有this,prototype的值传递给实例,其中prototype必须是key-value的形式才传递,就是实例化后的,
数组和object的关系就是都一致的。