What is the difference between the ways to implement inheritance in javascript.
see also :
http://www.w3school.com.cn/js/pro_js_inheritance_implementing.asp
http://davidshariff.com/blog/javascript-inheritance-patterns/
Object masquerading with Javascript
Often people follow this common approach to make their Javascript programs object oriented. Let me explain one more approach for creating subclasses.
Prototype Approach (Common)
Base Class
function Person(fname, lname){
this.fname = fname;
this.lname = lname;
}
Person.prototype.getFullName = function(){
return this.fname+” “+this.lname;
}
SubClass
function Employee(jobType){
this.jobType = jobType;
}
Employee.prototype = new Person();
Employee.prototype.getNameAndJobType = function(){
return this.fname+” “+this.lname+” “+this.jobType;
}
Cons
1. Every subclass of Person creates an instance of Person to assign to it’s prototype. Its an unnecessary overhead.
2. constructor of Employee’s instance is returned as Person. Weird!
3. Properties inherited from Person has to be assigned explicitly. You can’t create Employee instances like this. var employee = new Employee('Alice', 'Bob', 'Engineer');
instead you have to assign fname and lname explicitly. var employee = new Employee('Engineer');
employee.fname = 'Alice';
employee.lname = 'Bob';
Pros
1. employee instanceof Employee === employee instanceof Person === true
Object Masquerading Approach
Base Class
function Person(fname, lname){
this.fname = fname;
this.lname = lname;
}
Person.prototype.getFullName = function(){
return this.fname+” “+this.lname;
}
SubClass
function Employee(fname, lname, jobType){
this.jobType = jobType;
Person.call(this, fname, lname);
}
Employee.prototype.getNameAndJobType = function(){
return this.fname+” “+this.lname+” “+this.jobType;
}
Pros
1. You don’t need to create Person instance for every subclass of Person
2. constructor of Employee’s instance is returned as Employee. Bingo!
3. You can directly make Employee instance by [var employee = new Employee('Alice', 'Bob', 'Engineer')]
Cons
1. employee instanceof Employee === true BUT employee instanceof Person === false
2. In this approach an employee instance doesn’t has access to Person’s method if the method is defined in prototype instead of inside constructor definition. var employee = new Employee('Alice', 'Bob', 'Engineer');
employee.getFullName(); //Doesn't work
After subclassing Person, you need to copy all the methods from Person’s prototype to Employee’s prototype. function copyPrototype(from, to){
var fromPrototype = from.prototype;
var toPrototype = to.prototype;
for(o in fromPrototype){
if(fromPrototype.hasOwnProperty(o)){
toPrototype[o] = fromPrototype[o];
}
}
}
copyPrototype(Person, Employee);
Note: Employee instance would have access to Person’s method if it was defined inside function instead or prototype. But still, you should always define methods in prototype instead of constructor. Defining methods inside Constructor makes instances heavy.
What is the difference between the ways to implement inheritance in javascript.的更多相关文章
- Zero Copy I: User-Mode Perspective
By now almost everyone has heard of so-called zero-copy functionality under Linux, but I often run i ...
- Raft
http://thesecretlivesofdata.com/raft/ https://github.com/coreos/etcd 1 Introduction Consensus algo ...
- JavaScript Madness: Dynamic Script Loading
Introduction I've developed some pretty seriously Javascript intensive sites, where the sheer quanti ...
- JavaScript Garden
Objects Object Usage and Properties Everything in JavaScript acts like an object, with the only two ...
- The top 100 papers Nature explores the most-cited research of all time.
The top 100 papers Nature explores the most-cited research of all time. The discovery of high-temper ...
- 转载:10 Easy Steps to a Complete Understanding of SQL
10 Easy Steps to a Complete Understanding of SQL 原文地址:http://tech.pro/tutorial/1555/10-easy-steps-to ...
- Git 少用 Pull 多用 Fetch 和 Merge(转)
英文原文:git: fetch and merge, don’t pull This is too long and rambling, but to steal a joke from Mark T ...
- 10 Easy Steps to a Complete Understanding of SQL
原文出处:http://tech.pro/tutorial/1555/10-easy-steps-to-a-complete-understanding-of-sql(已经失效,现在收集如下) Too ...
- springmvc4开发rest
Spring MVC 4 RESTFul Web Services CRUD Example+RestTemplate Created on: August 11, 2015 | Last upd ...
随机推荐
- 十天精通CSS3学习笔记 part4
CSS3中的变形与动画(下) CSS3 Keyframes介绍 Keyframes 被称为关键帧,其类似于Flash中的关键帧.在CSS3中其主要以"@keyframes"开头,后 ...
- 即时反应的input和propertychange方法
在web开发中,我们有时会需要动态监听输入框值的变化,当使用onkeydown.onkeypress.onkeyup作为监听事件时,会发现一些复制粘贴等操作用不了,同时,在处理组合快键键的时候也很麻烦 ...
- 如何用JS判断推广链接所属的客服
今天有一个客户提出一个需求:网站有多个在线客服,每个客服都有自己的网站推广链接,当访客通过该客服的推广链接进入网站时,必须指定由该客服接待. 我的实现思路是获取推广链接中特定字符,然后判断字符对应的客 ...
- How to run a geoprocessing tool
How to run a geoprocessing tool In this topic Running a geoprocessing tool Toolbox names and namespa ...
- YY前端课1
1. HTML页面主要由声明.头部.主体三部分构成. 2. 头部及其meta标签,以及主体的title标签很重要,对SEO.搜索优化等很重要. 3. 常用的charset编码格式有2种,一种是utf- ...
- 关于/usr/local/lib/libz.a(zutil.o): relocation R_X86_64_32 against `.rodata.str1.1' can not be used when making a shared object; recompile with -fPIC解决办法
具体报错截图如下: 解决方法: 题外话,我对makefill cmake也是一窍不通因此本人也是不想去积极的解决这个问题,但是当你求助无缘的时候你才会静心去思考.读到这句话的时候也许你已经发现了问题所 ...
- FT部署图
- DNS bind子域授权安装
失败经验:rhel 6.x bind 9.8,两台做子域授权,最后失败.原因不详. 改用rhel 5.5, bind 9.3,同样的配置,就成功了.具体记录一下9.3的配置. 安装:采用安装RHEL时 ...
- 搭建apache http服务器
异步: http://blog.csdn.net/lzhlzz/article/details/39496285
- GestureDetectorl监听
package com.example.gesturedetectorinterface; /** * write by harvic * 2014-9-25 * blog.csdn.net/harv ...