首页 > 编程语言 > 详细

javascript继承方式.

时间:2017-11-21 16:25:55      阅读:194      评论:0      收藏:0      [点我收藏+]
 
//原型链继承
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继承方式.

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!