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 ...
随机推荐
- Unity3D 中的协程
若干文章: 1.Coroutine,你究竟干了什么? 2.Radical Coroutines 3.Extended Unity Coroutines
- 虚拟化--IO虚拟化基本原理
本文话题: IO虚拟化概述 设备发现 访问截获 设备模拟 设备共享基于软件的IO虚拟化 基于前端后端的IO虚拟化基于硬件的IO虚拟化 概述 从处理器的角度看,外设是通过一组I/O资源(端口I/O或者是 ...
- I:trainage Ditches
总时间限制: 1000ms 内存限制: 65536kB描述Every time it rains on Farmer John's fields, a pond forms over Bessie's ...
- jmeter 内存溢出解决方法
执行“评论新鲜事”200并发就内存溢出 解决方法: [caozijuan@test09 bin]$ vi jmeter JVM_ARGS="-Xms1024m -Xmx4096m" ...
- powerdesigner,eclipse整合安装
com.sybase.powerdesigner.eclipse.link path=D:\\dbs\\dbtools\\SAP\\PowerDesigner16
- PHP 数据安全问题总结
总结:关键的判断,比较尽量使用=== 类型和值都比较的恒等比较 1.if($var) $var 遵循boolean 转换. 当转换为 boolean 时,以下值被认为是 FALSE: 布尔值 FALS ...
- ORACLE 连接SQLSERVER 数据库备忘
最近工作需要,要从SQL SERVER数据库中同步提取数据. 这里采用了 Oracle Gateway 来连接,折腾了半天,终于搞定,记录下已备下次使用. 基本资料网上都可以搜很多,官网配置说明在这 ...
- C——没有bool的C语言?
bool static my_var_initialized = false; 偶然写出了这样一句C代码,环境是visual studio 2012,工程是Compile as C的,竟然报了好几个错 ...
- tfs中如何创建团队项目及如何操作团队项目
创建团队项目集合 tfs server管理控制台\团队项目集合页面.选择'创建集合'链接,按向导即可创建项目集合. 创建团队项目 创建好团队项目集合后,就要开始创建团队项目了. 进入vs,连接上tfs ...
- 学习docker
虚拟机下Ubuntu环境 1.sudo apt-get update 2.sudo apt-get install docker.io 3.在daocloud(http://www.daocloud. ...