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.的更多相关文章

  1. 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 ...

  2. Raft

    http://thesecretlivesofdata.com/raft/ https://github.com/coreos/etcd   1 Introduction Consensus algo ...

  3. JavaScript Madness: Dynamic Script Loading

    Introduction I've developed some pretty seriously Javascript intensive sites, where the sheer quanti ...

  4. JavaScript Garden

    Objects Object Usage and Properties Everything in JavaScript acts like an object, with the only two ...

  5. 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 ...

  6. 转载: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 ...

  7. 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 ...

  8. 10 Easy Steps to a Complete Understanding of SQL

    原文出处:http://tech.pro/tutorial/1555/10-easy-steps-to-a-complete-understanding-of-sql(已经失效,现在收集如下) Too ...

  9. springmvc4开发rest

    Spring MVC 4 RESTFul Web Services CRUD Example+RestTemplate Created on:  August 11, 2015  | Last upd ...

随机推荐

  1. ps切图抠图详解-web前端(转)

    网页设计在技术层面上,第一步是美工做出网页效果图,第二步就是网页前端进行网页切图.网页切图工具常用的有fireworks.PS,这里使用PS进行网页切图. 我们通过设计稿,得到我们想要的产出物(如.p ...

  2. TensorFlow之Varibale 使用方法

    ------------------------------------------- 转载请注明: 来自博客园 xiuyuxuanchen 地址:http://www.cnblogs.com/gre ...

  3. Migrating an ASP.NET MVC application to ADFS authentication

    I recently built an ASP.NET application at work to help track internal use of our products. It's bee ...

  4. 239. Sliding Window Maximum *HARD* -- 滑动窗口的最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  5. CentOS5.4 搭建Hadoop2.5.2伪分布式环境

    简介: Hadoop是处理大数据的主要工具,其核心部分是HDFS.MapReduce.为了学习的方便,我在虚拟机上搭建了一个伪分布式环境,来进行开发学习. 一.安装前准备: 1)linux服务器:Vm ...

  6. BZOJ2329 [HNOI2011]括号修复

    把左括号看做$1$,右括号看做$-1$,于是查询操作等于查询一个区间左边右边最大(最小)子段和 支持区间翻转,反转,覆盖操作...注意如果有覆盖操作,之前的操作全部作废了...于是在下传标记的时候要最 ...

  7. Jesen不等式

  8. How to Develop blade and soul Skills

    How to Develop Skills Each skill can be improved for variation effects. Some will boost more strengt ...

  9. [CentOS] 指定命令别名:Alias & 软链接生成命令 ln -s

    参考:CentOS里alias命令详解 每天一个linux命令(35):ln 命令 1. Alias命令 功能描述:我们在进行系统的管理工作一定会有一些我们经常固定使用,但又很长的命令.那我们可以给这 ...

  10. 金山软件wps2012-2013通杀0day

    #!/usr/bin/python # Exploit Title: Kingsoft Office Writer v2012 8.1.0.3385 .wps Buffer Overflow Expl ...