你的位置:首页 > 软件开发 > Java > 对对象方法,类方法,原型方法的理解

对对象方法,类方法,原型方法的理解

发布时间:2017-08-31 16:00:10
function People(name){ this.name=name; //对象方法 this.Introduce=function(){ alert("My name is "+this.name); ...

对对象方法,类方法,原型方法的理解

 

function People(name)
{
  this.name=name;
  //对象方法
  this.Introduce=function(){
    alert("My name is "+this.name);
  }
}
//类方法
People.Run=function(){
  alert("I can run");
}
//原型方法
People.prototype.IntroduceChinese=function(){
  alert("我的名字是"+this.name);
}

 

//测试

var p1=new People("Windking");

p1.Introduce();

People.Run();

p1.Run(); //error p1没有Run()方法

p1.IntroduceChinese(); 

 

对象方法是可以继承的即父元素一份子元素也有一份,如果实例很多则很浪费内存空间

类方法不被继承只能类自身调用

原型方法可被继承但是他是一个引用类型,只有一份,即使有很多实例,但都是针指向prototype一个

 

prototype

 

 

function Person(name){    this.name=name;   }      Person.prototype.share=[];      Person.prototype.printName=function(){    alert(this.name);   }      var person1=new Person('Byron');   var person2=new Person('Frank');      person1.share.push(1);   person2.share.push(2);   console.log(person2.share); //[1,2]

 对对象方法,类方法,原型方法的理解

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:对对象方法,类方法,原型方法的理解

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

可能感兴趣文章

我的浏览记录