JavaScript prototype原型用法

JavaScript对象原型所有JavaScript对象都从原型继承属性和方法。型用functionPerson(first,型用 last, age, eye){ this.firstName = first;this.lastName = last;this.age = age;this.eyeColor = eye;}var myFather =newPerson("John","Doe",50,"blue");var myMother =newPerson("Sally","Rally",48,"green"); document.getElementById("demo").innerHTML ="My father is "+ myFather.age +". My mother is "+ myMother.age;

我们还了解到,您无法向现有对象构造函数添加新属性:

JavaScript prototype原型用法

functionPerson(first,型用 last, age, eye){ this.firstName = first;this.lastName = last;this.age = age;this.eyeColor = eye;}Person.nationality ="English";var myFather =newPerson("John","Doe",50,"blue");var myMother =newPerson("Sally","Rally",48,"green"); document.getElementById("demo").innerHTML ="The nationality of my father is "+ myFather.nationality;

要向构造函数添加新属性,必须将其添加到构造函数:

JavaScript prototype原型用法

functionPerson(first,型用 last, age, eye){ this.firstName = first;this.lastName = last;this.age = age;this.eyeColor = eye;this.nationality ="English";}var myFather =newPerson("John","Doe",50,"blue");var myMother =newPerson("Sally","Rally",48,"green"); document.getElementById("demo").innerHTML ="我父亲的国籍是 "+ myFather.nationality +". 我母亲的云服务器国籍是: "+ myMother.nationality;

原型继承

JavaScript prototype原型用法

所有JavaScript对象都从原型继承属性和方法:

Object.prototype位于原型继承链的顶部:Date对象,Array对象和Person对象继承自Object.prototype。型用 *Date对象继承自Date.prototype*Array对象继承自Array.prototype*Person对象继承自Person.prototype

向对象添加属性和方法

有时,云服务器提供商型用您希望向给定类型的型用所有现有对象添加新属性(或方法)。有时您想要向对象构造函数添加新属性(或方法)。型用

使用原型属性

JavaScript prototype属性允许您向对象构造函数添加新属性:

functionPerson(first,型用 last, age, eyecolor){ this.firstName = first;this.lastName = last;this.age = age;this.eyeColor = eyecolor;}Person.prototype.nationality ="English";

JavaScript prototype属性还允许您向对象构造函数添加新方法:

functionPerson(first, last, age, eyecolor){ this.firstName = first;this.lastName = last;this.age = age;this.eyeColor = eyecolor;}Person.prototype.name =function(){ returnthis.firstName +" "+this.lastName;};

更好的原型对象的网站模板文章

热点
上一篇:付款完成后,您只需耐心等待,如果您注册成功,系统会提示您。这里需要注意的是,域名是一个即时产品,只有在最终付款成功时才能预订,注册成功后不能更改。
下一篇:2、定期提交和投标域名注册。例如,益华网络点击“立即预订”后,平台会抢先为客户注册域名。当然,一个域名可能会被多个客户预订,所以出价最高的人中标。