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. SignalR入门之多平台SignalR服务端

    之前创建SignalR服务端是基于Web应用程序而言的.那么能不能把SignalR服务端做成控制台应用程序.Winform或windows服务呢? 答案是肯定的. 之前尽管看起来好像是IIS和ASP. ...

  2. 算法实例-C#-快速排序-QuickSort

    算法实例 ##排序算法Sort## ### 快速排序QuickSort ### bing搜索结果 http://www.bing.com/knows/search?q=%E5%BF%AB%E9%80% ...

  3. 鼠标向下滑动加载div

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. 孙鑫MFC学习笔记18:ActiveX

    18 1.容器和服务器程序 2.InvalidateControl重绘控件 3. 4.GetBackColor获取背景色 5.GetForeColor获取前景色 6.TranslateColor进行颜 ...

  5. AFNetworking 3.1

    听说之后AFHttpWorking版本可能会影响到苹果的审核,今天下了最新版本的AFHttpWorking,并且做了简单的封装,我这里是通过cocoapods下载了两个工具 1=AFHttpWorki ...

  6. hdu-1856-More is better

    More is better Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 327680/102400 K (Java/Others) ...

  7. python补充最常见的内置函数

    最常见的内置函数是: print("Hello World!") 数学运算 abs(-5)                         # 取绝对值,也就是5 round(2. ...

  8. BOOtstrap源码分析之 tooltip、popover

    一.tooltip(提示框) 源码文件: Tooltip.jsTooltip.scss 实现原理: 1.获取当前要显示tooltip的元素的定位信息(top.left.bottom.right.wid ...

  9. 分享11个纯css完成的图片浏览器

    图片画廊用于在网站上显示系列图片,它已成为网站重要的组成部分.实现图片画廊有很多种方法,今天要与大家分享的是11个使用纯 CSS 实现的图片画廊,它们代码少,效果炫,加载速度快,希望能对大家有所帮助. ...

  10. oracle表的管理

    表名和列的命名规则 必须以字母开头: 长度不能超过30字符: 不能使用oracle的保留字: 只能使用如下字符:A-Z,a-z,0-9,$,#等:   数据类型: 字符型: char       定长 ...