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 ...
随机推荐
- Web之路笔记之四
2014秋季学期Web2.0课程作业 <Homework1 - Recipe> 给出内容的文本文档,根据要求编写html和css.基本上没有难点. 1. 需要添加标签栏名称前面的小图标,是 ...
- 通过form表单获取值
<!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title> ...
- FileFilter 遍历某个目录下文件名含有某个字符的文件
由于IIS版本的升级,造成了文件名中含有“+”的特殊字符的文件(多数是图片)在网页中不能被访问,于是必须查找当前目录下含有多少这样的文件,从而制定最佳的解决方案. 废话少说,直接上核心代码: publ ...
- html slelect 标签默认值
<select name="channelCode" id="channelCode" class="all_input" style ...
- 关于KINECT2 和ROS接口安装的问题
具体安装过程见此博客 http://www.itdadao.com/articles/c15a450477p0.html 感谢博主. 注意,在我自己的电脑上,最后测试的两条代码执行不了,即:1)ros ...
- CentOS7下zip解压和unzip压缩文件
1.安装zip.unzip应用. yum install zip unzip
- cacti结合nagios
使用系统ubuntu12.0.45 监控软件,cacti 使用的是源码安装系统自带的版本过低需要添加插件 nagios采用的系统自带版本 安装nagios apt-get install nagios ...
- java集合中的传值和传引用
在学习java集合过程中发现了传值和传引用的区别: 我们来看下面两句话 ●java集合就像一种容器,我们可以把多个对象(实际上是对象的引用),丢进该容器.(来自疯狂java讲义) ●当使用Iterat ...
- Asp.net页面引用SAP IQ 16 iAnywhere.Data.SQLAnywhere.V4.0.dll报错,语言文件没找到
参考http://sqlanywhere-forum.sap.com/questions/20420/saconnection-threw-an-exception-cannot-find-the-l ...
- Cogs 14. [网络流24题] 搭配飞行员
这道题其实蛮好想的,因为分为正,副飞行员.所以就把正飞行员当作Boy,副飞行员当作Girl.然后做Hungry即可. #include<bits/stdc++.h> using names ...