`

javascript inheritance

阅读更多
第一种方式:(YUI implement)
function extend(Child, Parent) {
  var F = function(){};
  F.prototype = Parent.prototype;
  Child.prototype = new F();
  Child.prototype.constructor = Child;
  Child.uber = Parent.prototype;
}


第二种方式:
function extend2(Child, Parent) {
  var p = Parent.prototype;
  var c = Child.prototype;
  for (var i in p) {
    c[i] = p[i];
  }
  c.uber = p;
}


第二种方式没有第一种方式效率高。
原因:properties of the child prototype are being duplicated instead of simply being looked up via the prototype chain during execution.
在执行期间,child原型的属性被复制了而不是通过原型链来进行查找。

Bear in mind that this is only true for properties containing primitive types. All objects (including functions and arrays) are not duplicated, because these are passed by reference only.

要注意:child原型中属性并不是全部被复制,如果属性是基本类型则被复制,如果属性是object类型(包括function和array)则只是一个引用(内存地址);

from:object-oriented-javascript
分享到:
评论

相关推荐

    解析John Resig Simple JavaScript Inheritance代码

    由于作者翻译会加入 自己的理解 以便自己学习和使用, 如果英文好的... 他为文章起名为”Simple JavaScript Inheritance” . 他使用的一些很巧妙的技术来实现 super 方法. 你还可以看原文也会有详细的说明, 他也在他

    JavaScript.Object.Programming.148421

    This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object ...

    JavaScript Object Programming(Apress,2015)

    This brief book explains the advantages of the object model, inheritance, both classical and prototypical, and shows how these concepts can be implemented in JavaScript. It also shows how object ...

    JavaScript中的类继承

    JavaScript Inheritance DouglasCrockfordwww.crockford.com And you think you’re so clever and classless and free–John Lennon JavaScript一种没有类的,面向对象的语言,它使用原型继承来代替类继承。这个...

    Advanced JavaScript (closures,prototype,inheritance)

    NULL 博文链接:https://butterflymacro.iteye.com/blog/1271789

    JavaScript 继承详解(五)

    在本章中,我们将分析John Resig关于JavaScript继承的一个实现 – Simple JavaScript Inheritance。  John Resig作为jQuery的创始人而声名在外。是《Pro JavaScript Techniques》的作者,而且Resig将会在今年秋天...

    JavaScript Data Structures and Algorithms

    JavaScript follows the prototypal inheritance pattern, unlike Java and C++ (which follow the inheritance pattern), there are some changes in writing data structures in JavaScript. The classical ...

    对javascript继承的理解

    更不用谈继承、多态了,为了模拟出一些其它面向对象编程语言的这些特性,有好多大牛写了给出了实现方式,看了John Resig的《Simple JavaScript Inheritance》这篇文章,深深被折服了,原来短短几十行javascript也...

    js-test-basic:JavaScript Test 基础训练

    ##Augment 简介The world's smallest and fastest classical JavaScript inheritance pattern, augment, is a seven line function which allows you to write CoffeeScript style classes with a flair of ...

    JavaScript Patterns

    * Learn the options available for code reuse and inheritance in JavaScript * Study sample JavaScript approaches to common design patterns such as Singleton, Factory, Decorator, and more * Examine ...

    The Principles of Object-Oriented JavaScript 1st Edition

    Zakas thoroughly explores JavaScript's object-oriented nature, revealing the language's unique implementation of inheritance and other key characteristics. You'll learn: –The difference between ...

    javascript面向对象编程指南 2nd

    Reuse code with common patterns for inheritance Make your programs cleaner, faster and compatible with other programs and libraries Use object-oriented JavaScript for improving script performance ...

    Programming JavaScript Applications

    Prototypal Inheritance, Prototype Cloning and the Flyweight Pattern, The Module Pattern, Unit Testing, Coming soon:, Architecting for Scale, Collaboration, Build, Continuous Integration, Deployment, ...

    Pro JavaScript Design Patterns

    including making JavaScript more expressive, inheritance, encapsulation, information hiding, and more. With that covered, you can kick-start your JavaScript development in the second part of the book...

    Javascript.Object.Oriented.Programming.pdf

    Build sophisticated web applications by mastering the art of Object-Oriented Javascript About This Book Learn popular Object-Oriented programming (OOP) principles and design patterns to build robust ...

    Speaking JavaScript

    Objects and Inheritance Chapter 18. Arrays Chapter 19. Regular Expressions Chapter 20. Dates Chapter 21. Math Chapter 22. JSON Chapter 23. Standard Global Variables Chapter 24. Unicode and JavaScript...

    Sammie Bae - JavaScript Data Structures and Algorithms - 2019.pdf

    Because JavaScript follows the prototypal inheritance pattern, unlike Java and C++ (which follow the inheritance pattern), there are some changes in writing data structures in JavaScript. The ...

    class-event:此类的对象可以触发事件,其他对象可以绑定到这些事件

    的Simple JavaScript Inheritance的原始想法和实现 对Q.Evented Class 的最初想法和实现 我只是提取了类代码并使其独立。 使用类事件 ClassEvent类将事件系统添加到基类中。 它提供了一种侦听和触发事件的机制。 ...

Global site tag (gtag.js) - Google Analytics