Advantage

Avoid re-created instance method to this inside of the constructor.

method() implementation

The method() takes two parameters:

• The name of the new method

• The implementation of the method

if (typeof Function.prototype.method !== "function") {

    Function.prototype.method = function (name, implementation) {

        this.prototype[name] = implementation;

        return this;

    };

}

How to use this pattern

var Person = function (name) {

    this.name = name;

}.method('getName', function () {

    return this.name;

}).method('setName', function (name) {

    this.name = name;

    return this;

}); 

var a = new Person('Adam');

a.getName(); // 'Adam'

a.setName('Eve').getName(); // 'Eve'

References: 

JavaScript Patterns - by Stoyan Stefanov (O`Reilly)

JavaScript Patterns 5.9 method() Method的更多相关文章

  1. JavaScript Patterns 6.7 Borrowing Methods

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

  2. JavaScript Patterns 6.5 Inheritance by Copying Properties

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

  3. JavaScript Patterns 6.4 Prototypal Inheritance

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

  4. JavaScript Patterns 6.3 Klass

    Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...

  5. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

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

  6. JavaScript Patterns 5.8 Chaining Pattern

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

  7. JavaScript Patterns 5.6 Static Members

    Public Static Members // constructor var Gadget = function (price) { this.price = price; }; // a sta ...

  8. JavaScript Patterns 5.3 Private Properties and Methods

    All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...

  9. JavaScript Patterns 4.10 Curry

    Function Application apply() takes two parameters: the first one is an object to bind to this inside ...

随机推荐

  1. 利用chardet检测网页编码

    环境:Win7_x64 + python3.4.3 需要先下载chardet并进行安装,下载地址:https://pypi.python.org/packages/source/c/chardet/c ...

  2. Castle ActiveRecord框架学习(二):快速搭建简单博客网站

    一.数据库 1.数据表 Category:类别标签表(字段Type=1为类别,Type=2为标签) Category_Post:类别标签与文章中间表 Post:文章表 Comment:评论表 2.数据 ...

  3. ASP.NET MVC 网站开发总结(六)——简谈Json的序列化与反序列化

    首先,先简单的谈一下什么是序列化与反序列化,序列化 (Serialization)将对象的状态信息转换为可以存储或传输的形式的过程.在序列化期间,对象将其当前状态写入到临时或持久性存储区.以后,可以通 ...

  4. 组件Newtonsoft.Json实现object2json转换

    很多情况下,我们需要把数据类型做一些转换,供其它外部的子系统调用. 最为典型的是生成json格式供javascript作调用. 现成的组件Newtonsoft.Json可以实现object2json之 ...

  5. Telerik UI For WinForms--关于RadGridView的列排序

    在使用RadGridView绑定数据后,我希望属性的显示顺序按继承层次显示,但实际是相反的.下面示例两个类: public class A { public string Astr { get; se ...

  6. LeetCode121:Best Time to Buy and Sell Stock

    题目: Say you have an array for which the ith element is the price of a given stock on day i. If you w ...

  7. pageEncoding的默认设置

    windows-->preference-->myeclipse-->files and editors-->jsp 右侧 Encoding 选择  ISO 10646/Uni ...

  8. python问题记录

    今天才python群里看到一个问题 python2.7: L = [x for x in 'hello'] print L print x python3.4: L = [ x for x in 'h ...

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

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

  10. 初学C++ 之 输入输出(IDE:vs2013)

    #include <iostream> //引用头文件(输入输出) using namespace std; //引用命名空间,方便使用输入输出语句 class MathMethod { ...