一、用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>

js继承的几种实现方法的更多相关文章

  1. JS面向对象(3) -- Object类,静态属性,闭包,私有属性, call和apply的使用,继承的三种实现方法

    相关链接: JS面向对象(1) -- 简介,入门,系统常用类,自定义类,constructor,typeof,instanceof,对象在内存中的表现形式 JS面向对象(2) -- this的使用,对 ...

  2. js面向对象初步探究(上) js面向对象的5种写方法

    非常长一段时间看网上大神的JS代码特别吃力.那种面向对象的写法方式让人看得云里来雾里去.于是就研究了一下JS面向对象.因为是初学,就将自己在网上找到的资料整理一下,作为记忆. js面向对象的5种写方法 ...

  3. JS中的五种去重方法

    JS中的五种去重方法 第一种方法: 第二种方法:  第三种方法: 第四种方法: 第五种方法:优化遍历数组法 思路:获取没重复的最右一值放入新数组 * 方法的实现代码相当酷炫,* 实现思路:获取没重复的 ...

  4. js继承的几种方法和es6继承方法

        一.原型链继     1.基本思想     利用原型链来实现继承,超类的一个实例作为子类的原型     2.具体实现     function F() {}     //原型属性,原型方法: ...

  5. js 继承的几种方式

    JS继承的实现方式: 既然要实现继承,那么首先我们得有一个父类,代码如下: function Animal(name) { // 属性 this.name = name || '小白'; // 实例方 ...

  6. js里面的三种注释方法

    javascript(js)语言里面的注释方法有三种. 第一种是多行注释"/**/",一般js文件开头,介绍作者,函数等信息. /* *author:xxx *day:2008-0 ...

  7. JS中的两种刷新方法以及区别和适用范围

    在项目中有一个人信息修改的页面,但是修改后显示的却是修改之前的内容,分析问题后发现查询语句写在了修改语句之前,有些某些需要又必须这么写,但是修改信息后先却显示之前的信息也太不科学了. 所以我就想用js ...

  8. JS去重的几种常见方法

    JS数组去重的几种常见方法 一.简单的去重方法 // 最简单数组去重法 /* * 新建一新数组,遍历传入数组,值不在新数组就push进该新数组中 * IE8以下不支持数组的indexOf方法 * */ ...

  9. 实现JS继承的几种方法

    总的来说,JS的继承大体上分为两种:借用构造函数方式和原型方式 首先,我们来看看借用构造函数方式的几种做法: //方式一function Person(name, sex){ this.name = ...

随机推荐

  1. laravel Schema 查看索引是否存在

    Schema::connection('')->table($tableName, function (Blueprint $table) { $sm = Schema::getConnecti ...

  2. My thoughts after NOIP 2018(1)

    联赛就这样结束了. 感觉真是奇怪啊.以前看重的东西,像是忽然扔下的包袱,一下子轻了. 而我一直在逃避的,不愿直面的东西,果然终于还是要再次面对了啊. 文化课百废待兴,\(noip\)的最终结果依然未知 ...

  3. 使用Nginx反向代理和proxy_cache缓存搭建CDN服务器加快Web访问速度

    碰到问题:移动用户访问web服务器www.osyunwei.com很慢解决办法:1.在移动机房放置一台nginx反向代理服务器2.通过域名DNS智能解析,所有移动用户访问www.osyunwei.co ...

  4. java代码示例(7-1)

    Dog类 /*** * 宠物狗狗类,使用权限修饰符private和public进行封装 * @author chenyanlong * 日期:2017/10/15 */ package com.hp. ...

  5. db.properties文件的配置格式

    #加载驱动 mysql.driver=com.mysql.jdbc.Driver #加载数据库 mysql.url=jdbc:mysql://localhost:3306/floor_shop #用户 ...

  6. 一个奇怪的SystemClock_Config问题解决方法

    一直在SystemClock_Config程序死掉,然后下载最新的STm32Cube从新创建项目成功了.

  7. 向GitHub上上传代码(转)

    使用git将项目上传到github(最简单方法)   首先你需要一个github账号,所有还没有的话先去注册吧! https://github.com/ 我们使用git需要先安装git工具,这里给出下 ...

  8. [C++]栈区(栈)与堆区(类链表)[转/摘]

     一.预备知识—程序的内存分配   一个由C/C++编译的程序占用的内存分为以下几个部分    1.栈区(stack)—   由编译器自动分配释放   ,存放函数的参数值,局部变量的值等.其    操 ...

  9. Python实现图片压缩

    项目中大量用到图片加载,由于图片太大,加载速度很忙,因此需要对文件进行统一压缩 一:导入包 from PIL import Image import os 二:获取图片文件的大小 def get_si ...

  10. 课程5:Spring框架2016版视频--视频列表目录

    \day01视频\01-今天内容介绍.avi; \day01视频\02-spring的相关概念.avi; \day01视频\03-spring的ioc底层原理(一).avi; \day01视频\04- ...