1 var o = {a: 1};
2 var o2=new Object();
3 o2.a=1;
function parent(name,age){
var Child = Object();
Child.name=name;
Child.age=age;
Child.sayHi=(){
console.log("Hi");
}
return Child;
};
var x = Parent("Tom",12);
console.log(x.name); //Tom
x.sayHi(); Hi
Parent(name,1)">this.name=name;
this.age=age;
this.sayHi=);
};
}
var x = new Parent("Tom",1)">Hi
name;
Parent.prototype.age=age;
Parent.prototype.sayHi=Hi
age;
Parent.prototype.arr=["123","we"];
Parent.prototype.sayHi=);
var y = new Parent("Tom1",1)">);
x.arr.push("x");
y.arr.push("y");
console.log(x.arr);["123","we","x","y"]
console.log(y.arr);
//只把属性留在这里定义,方法放在原型对象中
this.name=name;
this.age=age;
}
第一种方式
Parent.prototype.sayHi=(){
console.log("Hi");
};
第二种方式
//由于采用对象字面量,因此必须修正其constructor属性;
Parent.prototype={
constructor:Parent,sayHi:);
}
}
Hi
age;
if( typeof this.sayHi !="function"){
Parent.prototype.sayHi=(){
console.log("Hi");
};
}
}
Hi
var o= Object();
//私有变量或者方法
var name=name,age=age;
o.sayName=(){
//name前面没有this
console.log(name+" "+age)
}
o;
}
);
x.sayName(); Tom 12