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. 十天精通CSS3学习笔记 part3

    第8章 CSS3中的变形与动画(上) 变形--旋转 rotate() 旋转rotate()函数通过指定的角度参数使元素相对原点进行旋转.它主要在二维空间内进行操作,设置一个角度值,用来指定旋转的幅度. ...

  2. Jquery想说爱你不容易

    JQuery是一套跨浏览器的JavaScript库,简化HTML与JavaScript之间的操作.由John Resig在2006年1月的BarCamp NYC上发布第一个版本.目前是由 Dave M ...

  3. JS/jquery获取iframe内部元素和ifame中获取外部元素精华

    1.从外部获取iframe内部元素方法: js : window.frames['frame'].document.getElementById("imglist");   //f ...

  4. RPM

    1.安装RPM 使用-ivh选项即可,能查看安装信息和进度. 例如: #RPM -ivh XXX.rpm RPM升级与更新,使用-Uvh选项或者-Fvh选项,两者略有区别. -Uvh选项:后面接的软件 ...

  5. spark发行版笔记10

    感谢DT大数据梦工厂支持提供技术支持,DT大数据梦工厂专注于Spark发行版定制. 本期概览: 数据接收全生命周期的思考 大数据处理框架中,最重要的就是性能,性能是排在前面的.其次再考虑其他的.因为数 ...

  6. PHP数学函数

    Abs: 取得绝对值. Acos: 取得反余弦值. Asin: 取得反正弦值. Atan: 取得反正切值. Atan2: 计算二数的反正切值. base_convert: 转换数字的进位方式. Bin ...

  7. 【流程管理】【PCB】PCB设计流程

    添加封装 封装库用官方库,如没有添加补丁库,用原库或其他库中元件复制修改 调用封装时可先放置到PCB里进行测量 3D模型添加网站 封装库分类按厂商分类,常用器件按器件类型分类, 命名使用规范 导入PC ...

  8. Devexpress - office - 效果

    项目开发时需要设计好看的UI界面,因公司使用Devexpress控件,因此用到了Devexpress自带的office效果 具体案例 新建一个RibbonForm模版 删除clientPanel(不删 ...

  9. JSTL跳出<c:forEach>循环

    <c:forEach items="${consultPager.dataList }" var="consult"> <tr> < ...

  10. OC NSString 基本操作(用到补充持续更新)

    1.将字符串拆分成数组 NSString *string = @"1,2,3,4"; NSArray *array = [string componentsSeparatedByS ...