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 ...
随机推荐
- 一步一步搭建客服系统 (6) chrome桌面共享
本文介绍了如何在chrome下用webrtc来实现桌面共.因为必要要用https来访问才行,因此也顺带介绍了如何使用SSL证书. 1 chrome扩展程序 先下载扩展程序示例: https://git ...
- Java 中的反射机制
JAVA反射机制 JAVA反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法:对于任意一个对象,都能够调用它的任意一个方法:这种动态获取的信息以及动态调用对象的方法的功能称为ja ...
- django CSRF token missing or incorrect
django 异步请求时提示403 按照一般情况权限问题,python文件没有问题,仔细看了下response里有一句 CSRF token missing or incorrect.这个肯定是因为安 ...
- NABCD竞争性需求分析
1. Need 需求 市面上关于各类记事本,时钟显示程序,图片显示程序,日历程序有很多,但是它们大多是零散的软件,不利于现在高集成的时代,我们所做的就是将各种功能合起来. 2. Approach ...
- 【译】UNIVERSAL IMAGE LOADER. PART 3---ImageLoader详解
在之前的文章,我们重点讲了Android-Universal-Image-Loader的三个主要组件,现在我们终于可以开始使用它了. Android-Universal-Image-Loader有四个 ...
- Counting-Sort
Counting-Sort(A,B,k) let C[0..k] be a new array for i = 0 to k C[i] = 0 for j = 1 to A.length C[A[j] ...
- paip.配置ef_unified_filter() failed ext_filter_module mod_ext_filter.so apache 错误解决
paip.配置ef_unified_filter() failed ext_filter_module mod_ext_filter.so apache 错误解决 作者Attilax 艾龙, ...
- java基础类和对象-题
1.创建一个三角形类,成员变量三边,方法求周长,创建类主类A来测试它. public class Sanjiaoxing { //定义属性 private int a; private int b; ...
- 如何保证access_token长期有效
为了使第三方开发者能够为用户提供更多更有价值的个性化服务,微信公众平台开放了许多接口,包括自定义菜单接口.客服接口.获取用户信息接口.用户分组接口.群发接口等,开发者在调用这些接口时,都需要传入一个相 ...
- Leetcode 102 Binary Tree Level Order Traversal 二叉树+BFS
二叉树的层次遍历 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...