js中原型式继承和类式继承

来源:互联网 发布:国外域名注册价格对比 编辑:程序博客网 时间:2024/05/20 05:29

其中的代码示例来自于http://web.jobbole.com/83319/博文

类式继承

在子函数中通过call和apply调用父函数的构造函数

栗子:

var father = function() {  this.age = 52;  this.say = function() {    alert('hello i am '+ this.name ' and i am '+this.age + 'years old');  }}var child = function() {  this.name = 'bill';  father.call(this);}var man = new child();man.say();

原型式继承

通过原型链来实现继承,将子函数的prototype赋值为父函数实例化对象,举个栗子:

var father = function() {}father.prototype.a = function() {}var child = function(){}//开始继承child.prototype = new father();var man = new child();man.a();



0 0