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. bean生命周期

    一.Bean的定义Spring通常通过配置文件定义Bean.如:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:s ...

  2. Groovy中文教程(链接收藏)

    学习Gradle前,需要有一个Groovy语言的基础,以免被Groovy的语法困扰,反而忽略了Gradle的知识.这里有一个Groovy的简明中文教程文档,可以快速学习Groovy的一些语法:http ...

  3. angularJs,ionic字符串操作

    1.首先我们需要把一段"文本或字符串"中的我们想进行操作的"字符串","字"筛选出来,代码如下: app.filter('replaceCo ...

  4. [原创]WPF应用MediaPlayer播放声音断续、不全解决方案

    1.检查扬声器和驱动程序. 测试方法:首先,应用Windows Media Player播放器播放,看是否有问题,如果有问题,基本断定是驱动程序问题.其次,点击扬声器,选择测试,查看扬声器是否好用,如 ...

  5. 微信JS SDK Demo 官方案例[转]

    摘要: 微信JS-SDK是微信公众平台面向网页开发者提供的基于微信内的网页开发工具包. 通过使用微信JS-SDK,网页开发者可借助微信高效地使用拍照.选图.语音.位置等手机系统的能力,同时可以直接使用 ...

  6. 揭秘JavaScript中谜一样的this

      揭秘JavaScript中谜一样的this 在这篇文章里我想阐明JavaScript中的this,希望对你理解this的工作机制有一些帮助.作为JavaScript程序员学习this对于你的发展有 ...

  7. PHP注册审核做法

    1.注册页面 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...

  8. sql 多行转换为一行

    select 字段1, [val]=( select 字段2 +',' from 表名 as b where b.字段1 = a.字段1  for xml path('')) from 表名 as a ...

  9. ImageLoader1

    package com.bawei.activity; import android.app.Activity; import android.graphics.Bitmap; import andr ...

  10. sublime简书安装配置

    sublime-text3编辑器 安装 sudo add-apt-repository ppa:webupd8team/sublime-text-3 sudo apt-get update sudo ...