Commonalities

• There’s a convention on how to name a method, which is to be considered the constructor of the class.

• Classes inherit from other classes.

• There’s access to the parent class (superclass) from within the child class.

The function takes two parameters: a parent class to be inherited and implementation of the new class provided by an object literal.

1. A  Child()  constructor function is created. This is the function that will be returned at the end and will be used as a class. In this function the __construct method is called if it exists. Also before that the parent’s  __construct is called (again, if it exists)  using  the  static  uber property.  There  might  be  cases  when uber is  not defined—when  you  inherit  from  Object for  example,  as  the  case  was  with  the Man class definition.

2. The second part takes care of the inheritance bit. It’s simply using the classical inheritance’s Holy Grail pattern. There’s only one new thing: setting the Parent to Object if no  Parent was passed to inherit from.

3. The  final  section  is  looping  through  all  the  implementation  methods  (such  as __construct and getNam ein the examples), which are the actual definition of the class and adding them to the prototype of Child.

var klass = function (Parent, props) {

    var Child, F, i;

    // 1. new constructor

    Child = function () {

        if (Child.uber && Child.uber.hasOwnProperty("__construct")) {

            Child.uber.__construct.apply(this, arguments);

        }

        if (Child.prototype.hasOwnProperty("__construct")) {

            Child.prototype.__construct.apply(this, arguments);

        }

    };

    // 2. inherit

    Parent = Parent || Object;

    F = function () {};

    F.prototype = Parent.prototype;

    Child.prototype = new F();

    Child.uber = Parent.prototype;

    Child.prototype.constructor = Child;

    // 3. add implementation methods

    for (i in props) {

        if (props.hasOwnProperty(i)) {

            Child.prototype[i] = props[i];

        }

    }

    // return the "class"

    return Child;

};

// The demo for the 6.2 Klass

var Man = klass(null, {

    __construct: function (what) {

        showMsg("Man's constructor");

        this.name = what;

    },

    getName: function () {

        return this.name;

    }

});

var SuperMan = klass(Man, {

    __construct: function (what) {

        showMsg("SuperMan's constructor");

    },

    getName: function () {

        var name;

        if (SuperMan.uber.hasOwnProperty("getName")) {

            name = SuperMan.uber.getName.call(this);

        }

        return "I am " + name;

    }

});

function showMsg(msg) {

    $('#msg').append(msg).append('<br/>');

}

$(function () {

    var clark = new SuperMan('Clark Kent');

    showMsg(clark.getName());

    // "I am Clark Kent"

});

Advantage

This pattern allows you to forget about the prototypes completely, and the good thing is you can tweak the syntax and the conventions

to resemble another of your favorite languages.

Disadvantage

It brings the whole confusing notion of classes, which don’t technically exist in the language. It adds new syntax and new rules to learn and remember.

References: 

JavaScript Patterns - by Stoyan Stefanov (O`Reilly)

JavaScript Patterns 6.3 Klass的更多相关文章

  1. JavaScript Patterns 7.1 Singleton

    7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. Th ...

  2. JavaScript Patterns 6.7 Borrowing Methods

    Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...

  3. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  4. JavaScript Patterns 6.5 Inheritance by Copying Properties

    Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...

  5. JavaScript Patterns 6.4 Prototypal Inheritance

    No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...

  6. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

    // the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...

  7. JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns

    In Java you could do something like: Person adam = new Person(); In JavaScript you would do: var ada ...

  8. JavaScript Patterns 5.9 method() Method

    Advantage Avoid re-created instance method to this inside of the constructor. method() implementatio ...

  9. JavaScript Patterns 5.8 Chaining Pattern

    Chaining Pattern - Call methods on an object one after the other without assigning the return values ...

随机推荐

  1. .net微信公众号开发——消息与事件

    作者:王先荣    本文介绍如何处理微信公众号开发中的消息与事件,包括:(1)消息(事件)概况:(2)验证消息的真实性:(3)解析消息:(4)被动回复消息:(5)发送其他消息.    开源项目地址:h ...

  2. iOS阶段学习第19天笔记(协议-Protocol)

    iOS学习(OC语言)知识点整理 一.关于协议(Protocol)的介绍 1)概念:协议指多个对象之间协商的一个接口对象,协议提供了一些方法用在协议的实现者和代理者      之间通讯的一种方式 2) ...

  3. WebResource.axd 404 错误

    再对程序进行发布的时候,出现以下情况 复制出错误信息: 网页错误详细信息 用户代理: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Triden ...

  4. 2016 大连网赛---Weak Pair(dfs+树状数组)

    题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5877 Problem Description You are given a rooted ...

  5. java.lang.IncompatibleClassChangeError: Implementing class的解决办法,折腾了一天总算解决了

    一,问题产生背景 git更新代码重启服务器后,问题就莫名奇妙的产生了,一看报错信息,基本看不懂,然后上百度去查,基本都是说jar包冲突,于是把矛头指向maven 二,问题的解决过程 既然确定了是mav ...

  6. CentOS安装zip unzip命令

    yum install zip unzip

  7. windows / linux系统中,端口被占用解决方法

    一.在windows操作系统中,查询端口占用和清除端口占用的程序 提升权限后用:netstat -b或用 1.查询端口占用的进程ID 点击"开始"-->"运行&qu ...

  8. GJM : Python简单爬虫入门 (一) [转载]

    版权声明:本文原创发表于 [请点击连接前往] ,未经作者同意必须保留此段声明!如有侵权请联系我删帖处理! 为大家介绍一个简单的爬虫工具BeautifulSoup BeautifulSoup拥有强大的解 ...

  9. asp.net mvc4 使用 System.Web.Optimization 对javascript和style的引入、代码合并和压缩的优化(ScriptBundle,StyleBundle,Bundling and Minification )

    Bundling and Minification两个单词对今天的内容有个比较好的总结. 问题所在 一. 在asp.net包括mvc项目中,引入js和css也许有人认为是个很容易和很简单操作的事情,v ...

  10. Codrops 实验:使用 Vibrant.js 提取图像颜色

    Codrops 分享了一个有趣的颜色提取实验.这个想法是创建图像的调色板,既有图像本身的潜移默化的影响,也有一些花哨的颜色延伸.通过使用 Vibrant.js 来提取图像中的颜色,并通过 CSS 过滤 ...