js继承的几种实现方法

一、用function实现:

复制代码
function Person(name) {
    this.name = name;
}
Person.prototype.getName = function() {
    return this.name;
}
function Author(name, books) {
    this.inherit=person;
    this.inherit(name);
    this.books = books;
    
}
var au=new Author("dororo","Learn much");
au.name
复制代码

或者同等效力的:

复制代码
function Person(name) {
    this.name = name;
}
Person.prototype.getName = function() {
    return this.name;
}
function Author(name, books) {
    Person.call(this, name);
    this.books = books;
    
}
var au=new Author("dororo","Learn much");
au.getName
复制代码

由于这只是将this作为参数,调用父类Person的构造函数,把赋予父类的所有域赋予Author子类,所以任何父类Person构造函数之外的定义的域(原型prototype),子类都不会继承。所以上面例子中,au.getName将是没有被定义的(undefined),因为getName是在Person的原型对象中定义的。

而且,子类的构造函数要在定义自己的域之前调用父类构造函数,免得子类的定义被父类覆盖掉。也就是说,Author定义属性book要在Person.call之后,否则会被Person中属性覆盖。同时,在子类中也最好不要用prototype来定义子类的函数域,因为在一个子类被new,实例化之后就要执行prototype,然后才是调用父类的构造函数,这样也容易被父类的属性覆盖掉。

二、用prototype实现:

复制代码
function Person(name) {
    this.name = name;
}
Person.prototype.getName = function() {
    return this.name;
}
function Author(name, books) {
    this.books = books;  
}
Author.prototype=new Person(name);
Author.prototype.constructor=Author;
Author.prototype.getBooks = function() {
    return this.books;
}
var au1=new Author("dororo1","Learn much");
var au2=new Author("dororo2","Learn less");
alert(au1.getName());
alert(au2.getName());
复制代码

这种方法避免了function实现中,无法继承prototype的问题。因为 Author.prototype=new Person(name);new Person()实例会调用Person构造和原型的所有属性。但是缺点是已经实例化了Author.prototype。所以当子类实例化的时候,所有非基本数据类型都是reference copy。所以上面例子中,无论实例au1,还是au2返回的值都是dororo1.

三、用“混合”实现

 

复制代码
function Person(name) {
    this.name = name;
}
Person.prototype.getName = function() {
    return this.name;
}
function Author(name, books) {
    this.base = new Person(name);
    for(var key in this.base){
        if(!this[key]){
           this[key]=this.base[key];
           }
           }
    this.book=books;
}
var au1=new Author("dororo1","work");
var au2=new Author("dororo2","play");
alert(au1.getName());
alert(au2.getName());
au1.book;
au2.book;
复制代码

 

属于扩展,把父类的所有域都拷贝到子类。完全没有上述两方面的问题。

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title></title> </head> <body> <script type="text/javascript">

 // ------------------------继承的第一种方式:对象冒充-----------------------------

 function Parent(name) { this.name = name; this.showInfo = function () { document.write(name); document.write("</br>"); } }

function Children(name, pwd) {//下面三行代码实现了子对象和父对象指向同一个引用//关键的代码this.method = Parent;this.method(name);delete this.method;

this.pwd = pwd;this.showMsg = function () {document.write(this.pwd);document.write("</br>");}}

var parent = new Parent("John");var child = new Children("Bob", 123);

parent.showInfo();child.showInfo();child.showMsg();

document.write("");document.write("</br>");

//call方法,是function里的一个方法。

//------------------------call方法简单示例-------------------------------

function test(str) {document.write(this.name + "," + str+"</br>");}

var person = new Object();person.name = "anllin";

//相当于调用了test方法test.call(person, "welcome"); //将person赋给了test里的this。

document.write("");document.write("</br>");

//--------------------继承的第二种方式,call方法------------------------------

function Father(name) {this.name = name;this.showName = function () {document.write(this.name + "</br>");}}

function Sub(name, pwd) {//关键的代码Father.call(this, name);

this.pwd = pwd;this.showPwd = function () {document.write(this.pwd + "<br>");}}

var father = new Father("Father");var sub = new Sub("Sub", 123456);father.showName();sub.showName();sub.showPwd();

document.write("");document.write("</br>");

//--------------------继承的第三种方式,apply方法------------------------------

function Mother(name) {this.name = name;this.showName = function () {document.write(this.name + "</br>");}}

function Daugther(name, pwd) {//关键的代码Mother.apply(this, new Array(name));

this.pwd = pwd;this.showPwd = function () {document.write(this.pwd + "<br>");}}

var mother = new Father("Mother");var daugther = new Sub("Daugther", 654321);mother.showName();daugther.showName();daugther.showPwd();

document.write("");document.write("</br>");

//--------------------继承的第四种方式,prototype chain方式------------------------------

//缺点:无法给构造函数传参数。

function Human(){ }

Human.prototype.name = "human";Human.prototype.showName = function () {document.write(this.name+"<br>");}

function Student() { }

//关键的代码Student.prototype = new Human();

Student.prototype.password = 456789;Student.prototype.showPwd = function () {document.write(this.password + "<br>");}

var human = new Human();var student = new Student();student.name = "student";human.showName();student.showName();student.showPwd();

document.write("");document.write("</br>");

//--------------------继承的第五种方式,混合方式------------------------------

function FatherClass(name) {this.name = name;}

FatherClass.prototype.showName = function () {document.write(this.name + "<br>");}

function SubClass(name, pwd) {

//关键的代码

FatherClass.call(this,name);this.pwd = pwd;}

//关键的代码

SubClass.prototype = new FatherClass;

SubClass.prototype.showPwd = function () {document.write(this.pwd + "<br>");}

var f = new FatherClass("FatherClass");var s = new SubClass("SubClass", 45678);f.showName();s.showName();s.showPwd();

</script> </body> </html>

posted @ 2015-05-12 15:30  对方屌丝正在输入中  阅读(229)  评论(0编辑  收藏  举报