//原型创建对象的几种方式
//1字面量形式
function People(){}
People.prototype={
name:"jerry",
age:"22",
sex:"男",
getname:function(){
return this.name+"创建成功...";
}
}
var peo1=new People();
alert(peo1.name);//jerry
alert(peo1.getname());//jerry创建成功
alert(peo1.constructor==People);//false
// 2构造函数创建对象
function People(){
People.prototype.name="jerry";
People.prototype.age="22";
People.prototype.getname=function(){
return this.name+"创建成功...";
}
}
var peo1=new People();
alert(peo1.name);//jerry
alert(peo1.getname());//jerry创建成功
alert(peo1.constructor==People);//true
// 区别:自变量创建的constructor属性不会指向对象的实例,而会指向object,构造函数则指向对象的实例
//3.字面量形式强制指向对象
function People(){}
People.prototype={
constructor:People,
name:"jerry",
age:"22",
sex:"男",
getname:function(){
return this.name+"创建成功...";
}
}
var peo1=new People();
alert(peo1.name);//jerry
alert(peo1.getname());//jerry创建成功
alert(peo1.constructor==People);//true
//以上创建对象的缺点:无法修改对象的值
//4.组合构造函数+原型模式(不变的部分用原型模式,变得东西用构造函数)
// 保持独立的构造函数
function Animal(name,age){
this.name=name;
this.age=age;
this.family=['小黄','小王','小旺'];
}
//保持共享的原型
Animal.prototype={
constructor:Animal,
eat:function(){
return (this.name+"eat.....");
}
}
var dog=new Animal('dog',2);
dog.family.push('小花');
alert(dog.name);//dog
alert(dog.family);//'小黄','小王','小旺','小花'
alert(dog.eat());//dog eat...
var pig=new Animal('pig',2);
alert(pig.name);//pig
alert(pig.family);//'小黄','小王','小旺'
alert(pig.eat());//pig eat...
//5组合构造函数,原型模式封装在一起:动态原型模式
function Animal(name,age){
this.name=name;
this.age=age;
this.family=['小黄','小王','小旺'];
alert("原型初始化开始");
Animal.prototype.eat=function(){
return (this.name+"eat.....");
}
alert("原型初始化结束");
}
// 原型初始化开始,原型初始化结束,原型初始化开始,原型初始化结束
var pig=new Animal('pig',2);
var cat=new Animal('cat',2);
// 原型初始化只需要初始化一次,修改eat方法
// 6组合构造函数,原型模式封装在一起:动态原型模式
function Animal(name,age){
this.name=name;
this.age=age;
this.family=['小黄','小王','小旺'];
if(typeof this.eat!="function"){
alert("原型初始化开始");
Animal.prototype.eat=function(){
return (this.name+"eat.....");
}
alert("原型初始化结束");
}
}
//原型初始化开始,原型初始化结束,
var pig=new Animal('pig',2);
var cat=new Animal('cat',2);
//1字面量形式
function People(){}
People.prototype={
name:"jerry",
age:"22",
sex:"男",
getname:function(){
return this.name+"创建成功...";
}
}
var peo1=new People();
alert(peo1.name);//jerry
alert(peo1.getname());//jerry创建成功
alert(peo1.constructor==People);//false
// 2构造函数创建对象
function People(){
People.prototype.name="jerry";
People.prototype.age="22";
People.prototype.getname=function(){
return this.name+"创建成功...";
}
}
var peo1=new People();
alert(peo1.name);//jerry
alert(peo1.getname());//jerry创建成功
alert(peo1.constructor==People);//true
// 区别:自变量创建的constructor属性不会指向对象的实例,而会指向object,构造函数则指向对象的实例
//3.字面量形式强制指向对象
function People(){}
People.prototype={
constructor:People,
name:"jerry",
age:"22",
sex:"男",
getname:function(){
return this.name+"创建成功...";
}
}
var peo1=new People();
alert(peo1.name);//jerry
alert(peo1.getname());//jerry创建成功
alert(peo1.constructor==People);//true
//以上创建对象的缺点:无法修改对象的值
//4.组合构造函数+原型模式(不变的部分用原型模式,变得东西用构造函数)
// 保持独立的构造函数
function Animal(name,age){
this.name=name;
this.age=age;
this.family=['小黄','小王','小旺'];
}
//保持共享的原型
Animal.prototype={
constructor:Animal,
eat:function(){
return (this.name+"eat.....");
}
}
var dog=new Animal('dog',2);
dog.family.push('小花');
alert(dog.name);//dog
alert(dog.family);//'小黄','小王','小旺','小花'
alert(dog.eat());//dog eat...
var pig=new Animal('pig',2);
alert(pig.name);//pig
alert(pig.family);//'小黄','小王','小旺'
alert(pig.eat());//pig eat...
//5组合构造函数,原型模式封装在一起:动态原型模式
function Animal(name,age){
this.name=name;
this.age=age;
this.family=['小黄','小王','小旺'];
alert("原型初始化开始");
Animal.prototype.eat=function(){
return (this.name+"eat.....");
}
alert("原型初始化结束");
}
// 原型初始化开始,原型初始化结束,原型初始化开始,原型初始化结束
var pig=new Animal('pig',2);
var cat=new Animal('cat',2);
// 原型初始化只需要初始化一次,修改eat方法
// 6组合构造函数,原型模式封装在一起:动态原型模式
function Animal(name,age){
this.name=name;
this.age=age;
this.family=['小黄','小王','小旺'];
if(typeof this.eat!="function"){
alert("原型初始化开始");
Animal.prototype.eat=function(){
return (this.name+"eat.....");
}
alert("原型初始化结束");
}
}
//原型初始化开始,原型初始化结束,
var pig=new Animal('pig',2);
var cat=new Animal('cat',2);