1.字面量
var obj={name:'TT',age:24}
2.new一个对象
function test(){}
test.prototype.z=3;
var obj=new test();
obj.x=1;
obj.y=2;
console.log(obj.x); //1
console.log(obj.y); //2
console.log(obj.z); //3
console.log('z' in obj); //true
console.log(obj.hasOwnProperty('z')); //false
for(let i in obj){
//console.log(i) //x y z
if (obj.hasOwnProperty(i)) {
console.log(i) //x y
}
}
tips:构造函数test拥有一个prototype属性,其实例对象obj的原型指向它构造函数的prototype属性
obj.__proto__===test.prototype; //true
再往上
obj.__proto__.__proto__ //Object.prototype
obj.__proto__.__proto__.__proto__ //null
简单理解这就是一条原型链
3.Object.create创建
var obj=Object.create({x:1});
obj.x //1
typeof obj.toString //"function"
obj.hasOwnPropery('x') //false
var obj=Object.create(null);
obj.toString //undefined
注意:
第一种 obj–>{x:1}–>Object.prototype–>null
第二种 obj–>null