码迷,mamicode.com
首页 > 编程语言 > 详细

javascript继承方式.

时间:2017-11-21 17:06:02      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:cti   函数   nbsp   log   var   parent   prot   this   prototype   

 
//原型链继承
function Parent() {
this.name = ‘per‘;
}
function Child() {
this.age = 20;
}
Child.prototype = new Parent();
var child = new Child();
console.log(child.name + " " + child.age)//per 20

//构造函数继承
function Parent(age) {
this.name = ‘person‘;
this.age = age;
}
function Child(age) {
Parent.call(this, age)
}
var child = new Child(20)
console.log(child.name + " " + child.age)//per 20

// 组合继承
function Parent(age) {
this.name = ‘per‘;
this.age = age;
}
Parent.prototype.sayAge = function () {
return this.name + ‘ age is ‘ + this.age;
}
function Child(age) {
Parent.call(this, age)
}
Child.prototype = new Parent();
var child = new Child(21);
console.log(child.sayAge())//per age is 21

javascript继承方式.

标签:cti   函数   nbsp   log   var   parent   prot   this   prototype   

原文地址:http://www.cnblogs.com/samsara-yx/p/7873700.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!