Javascript类继承-机制-代码Demo【原创】
最近看到《Javascript设计模式》,对js模拟的”继承方式“有了更深一步的了解,虽然之前也总是用到prototype、new ,但只是知其然不知所以然,现在将类继承的方法整理如下,暂时不对原型链、继承机制做过多描述,直接上代码,让大家先有一个整体的了解!
依照教程中对”类继承“逐步的优化,递进方式讲述了以下几种类继承方法,将教材中的几个demo例子记录一下,分享给大家,共同学习:
先给一个类定义与实例化的方法:
/* Class Person. */
//类定义
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
//实例化
var reader = new Person('John Smith');
reader.getName();
下面开始说继承,Author类继承Person类
方法1:
最基础的继承表达式
/* Class Author. */
function Author(name, books) {
Person.call(this, name); // Call the superclass' constructor in the scope of this.
this.books = books; // Add an attribute to Author.
}
Author.prototype = new Person(); // Set up the prototype chain.
Author.prototype.constructor = Author; // Set the constructor attribute to Author.
Author.prototype.getBooks = function() { // Add a method to Author.
return this.books;
};
//Author类实例化
var author = [];
author[0] = new Author('Dustin Diaz', ['JavaScript Design Patterns']);
author[1] = new Author('Ross Harmes', ['JavaScript Design Patterns']); author[1].getName();
author[1].getBooks();
方法2:
使用extend方法,将继承的方法封装进去
/* Extend function. */
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass;
}
/* Class Person. */
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
/* Class Author. */
function Author(name, books) {
Person.call(this, name); //该方法的问题:超类Person名字被固化在Author类的声明之中
this.books = books;
}
extend(Author, Person);
Author.prototype.getBooks = function() {
return this.books;
};
方法3:
解决方法2中问题(超类Person名字被固化在Author类声明中)
/* Extend function, improved. */
function extend(subClass, superClass) {
var F = function() {};
F.prototype = superClass.prototype;
subClass.prototype = new F();
subClass.prototype.constructor = subClass; //确保超类的constructor属性(superClass.prototype.constructor)已被正确设置
subClass.superclass = superClass.prototype;
if(superClass.prototype.constructor == Object.prototype.constructor) {
superClass.prototype.constructor = superClass;
}
}
/* Class Person. */
function Person(name) {
this.name = name;
}
Person.prototype.getName = function() {
return this.name;
}
/* Class Author. */
function Author(name, books) {
Author.superclass.constructor.call(this, name);
this.books = books;
}
extend(Author, Person);
Author.prototype.getBooks = function() {
return this.books;
};
//有了superclass属性,就可以直接调用超类中的方法
Author.prototype.getName = function() {
var name = Author.superclass.getName.call(this);
return name + ', Author of ' + this.getBooks().join(', ');
};
最后,针对方法3,看一下两个class的结构,一目了然:

转载,请注明地址:
http://www.cnblogs.com/qiongmiaoer/p/4599869.html
------------------------------------------------
博主经营一家发饰淘宝店,都是纯手工制作哦,开业冲钻,只为信誉!需要的亲们可以光顾一下!谢谢大家的支持!
店名:
小鱼尼莫手工饰品店
经营:
发饰、头花、发夹、耳环等(手工制作)
网店:
http://shop117066935.taobao.com/
---------------------------------------------------------------------
Javascript类继承-机制-代码Demo【原创】的更多相关文章
- JavaScript类继承, 用什么方法好
JavaScript类继承, 用什么方法好 一个实例: 基类Car: function Car(color, year) { this.name = "car"; this.col ...
- JavaScript中继承机制的模仿实现
首先,我们用一个经典例子来简单阐述一下ECMAScript中的继承机制. 在几何学上,实质上几何形状只有两种,即椭圆形(是圆形的)和多边形(具有一定数量的边).圆是椭圆的一种,它只有一个焦点.三角形. ...
- JavaScript类继承
和其他功能一样,ECMAScript 实现继承的方式不止一种.这是因为 JavaScript 中的继承机制并不是明确规定的,而是通过模仿实现的.这意味着所有的继承细节并非完全由解释程序处理.作为开发者 ...
- javascript类继承的一些实验
其实一开始编js没怎么用过对象,一般都用func,func,func···但是用多了,感觉代码一点都不美观,还要这里包一个函数,那里包一个函数,或者一直都是函数调用,不好看,而且一些重用的都要重写的话 ...
- 第7.7节 案例详解:Python类继承机制
本节实现一个类继承的小程序,下面一边结合代码一边介绍相关继承的知识.例子以车.汽车为例,车为父类.汽车为子类. 一. 定义父类Vehicle class Vehicle(): def __ ...
- JavaScript简洁继承机制实现(不使用prototype和new)
此方法并非笔者原创,笔者只是在前辈的基础上,加以总结,得出一种简洁实用的JavaScript继承方法. 传统的JavaScript继承基于prototype原型链,并且需要使用大量的new操作,代码不 ...
- Javascript 类继承
Js继承 JavaScript并不是真正面向对象的语言,是基于对象的,没有类的概念. 要想实现继承,可以用js的原型prototype机制或者用apply和call方法去实现 /** 声明一个基础父类 ...
- javascript类继承系列五(其他方式继承)
除了前面学习的三种继承外,还有另外三种:原型继承寄生继承,寄生组合继承都是以: function object(o) { function F() { } F.prototype = o; retur ...
- javascript类继承系列二(原型链)
原型链是采用最主要的继承方式,原理:每一个类(构造器,js中的function)都有一个原型属性(prototype)指向一个原型对象,原型对象有一个构造器(constructor),它又指回到fun ...
随机推荐
- ASP.NET MVC学习之过滤器篇(1)
一.前言 继前面四篇ASP.NET MVC的随笔,我们继续向下学习.上一节我们学习了关于控制器的使用,本节我们将要学习如何使用过滤器控制用户访问页面. 二.正文 以下的示例建立在ASP.NET MVC ...
- Python的with语句
写过多线程程序的人肯定对各种锁很熟悉,尤其是下面这种代码 def lock_usage: lock.Lock() if(...) : lock.Unlock() return lock.Unlock( ...
- [51单片机] EEPROM 24c02 + 数码管 + 中断 [统计开机次数]
>_<:24c02的SCL连P2.0;SDA连P2.1;WP接GND;P0接8位数码管的8针;P2.2连段码;P2.3连位码; >_<:delay.c #include &qu ...
- win32汇编hello world
下载:http://www.masm32.com/ 安装masm32 建一个Var.bat文件并运行 @echo offset include=E:\masm32\includeset lib=E:\ ...
- Node.js学习系列总索引
Node.js学习系列也积累了一些了,建个总索引方便相互交流学习,后面会持续更新^_^! 尽量写些和实战相关的,不讲太多大道理... Node.js学习笔记系列总索引 Nodejs学习笔记(一)--- ...
- atitit.提升软件开发的效率and 质量的那些强大概念and方法总结
atitit.提升软件开发的效率and 质量的那些强大概念and方法总结 1. 主流编程中三个最糟糕的问题 1 1.1. 从理解问题后到实现的时间很长 1 1.2. 理解和维护代码 2 1.3. 学 ...
- 【转】BitKeeper与Linux,git史前琐事
http://www.path8.net/tn/archives/6039 维持数年的BitKeeper与Linux的关系最终还是落入了好莱坞明星婚姻式的结局.他们曾经相得益彰,最后却走到这个遗憾的地 ...
- Express ejs 3.* layout.ejs
新版本改成了 <%- include file.ejs %> 具体使用方法如下:1. views文件夹 下新建header.ejs,插入代码 <html><head> ...
- Git中当add错误的时候怎么办?
傻傻分不清楚. “git add .”是我常用的添加命令,添加完后来个“git status ”总是有那么几次发现有不想添加的东西.好多人用reset,nonono,这样不好会有个head错误爆出. ...
- ELK——在 CentOS/Linux 把 Kibana 3.0 部署在 Nginx 1.9.12
上一篇文章<安装 logstash 2.2.0.elasticsearch 2.2.0 和 Kibana 3.0>,介绍了如何安装 Logstash.Elasticsearch 以及用 P ...