JavaScript2种构造函数创建对象的模式以及继承的实现

第一种模式:

function Person(){
}
Person.prototype.say=function(){
    alert('hello');
}
var person=new Person();
person.say();//hello

依据第一种模式说一下继承的实现:

function Person(){
}
Person.prototype.say=function(){
    alert('hello');
}
function Man(){}
Man.prototype=new Person()
var man=new Man();
man.say(); //hello

另外一种模式:

function Person(){
    var _this={};//创建一个空的对象
    _this.say=function(){alert('hello')};
    return _this;
}
function person=new Person();
person.say();//hello

另外一种模式的继承:

function Person(){
    var _this={};//创建一个空的对象
    _this.say=function(){alert('hello')};
    return _this;
}
function Man(){
    var _this=new Person();
    return _this;
}
var a=new Man();
a.say();//hello

本文作者:罗坚元

原文地址:https://www.cnblogs.com/slgkaifa/p/7207230.html