Public Static Members

// constructor

var Gadget = function (price) {

    this.price = price;

};

// a static method

Gadget.isShiny = function () {

    // this always works

    var msg = "you bet";

    // Checking if the static method is called by instance.

    if (this instanceof Gadget) {

        // this only works if called non-statically

        msg += ", it costs $" + this.price + '!';

    }

    return msg;

};

// a normal method added to the prototype

Gadget.prototype.setPrice = function (price) {

    this.price = price;

};

// a normal method added to the prototype

Gadget.prototype.isShiny = function () {

    return Gadget.isShiny.call(this);

};

// Attempting to call an instance method statically won’t work

typeof Gadget.setPrice; // "undefined"

Testing a static method call:

Gadget.isShiny(); // "you bet"

Testing an instance, nonstatic call:

var a = new Gadget('499.99');

a.isShiny(); // "you bet, it costs $499.99!"

Private Static Members

• Shared by all the objects created with the same constructor function

• Not accessible outside the constructor

// constructor

var Gadget = (function () {

    // static variable/property

    var counter = 0,

        NewGadget;

    // this will become the new constructor implementation

NewGadget = function () { counter += 1; };
// a privileged method NewGadget.prototype.getLastId = function () { return counter; }; // overwrite the constructor return NewGadget; }()); // execute immediately var iphone = new Gadget(); iphone.getLastId(); // var ipod = new Gadget(); ipod.getLastId(); // var ipad = new Gadget(); ipad.getLastId(); //

References: 

JavaScript Patterns - by Stoyan Stefanov (O`Reilly)

JavaScript Patterns 5.6 Static Members的更多相关文章

  1. JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance

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

  2. 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 ...

  3. JavaScript Patterns 6.3 Klass

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

  4. JavaScript Patterns 5.7 Object Constants

    Principle Make variables shouldn't be changed stand out using all caps. Add constants as static prop ...

  5. JavaScript Patterns 5.3 Private Properties and Methods

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

  6. JavaScript Patterns 6.7 Borrowing Methods

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

  7. JavaScript Patterns 6.6 Mix-ins

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

  8. JavaScript Patterns 6.5 Inheritance by Copying Properties

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

  9. JavaScript Patterns 6.4 Prototypal Inheritance

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

随机推荐

  1. jQuery漂亮图标的垂直导航菜单

    效果展示 http://hovertree.com/texiao/nav/3/ jQuery漂亮图标的垂直导航菜单 是一款当鼠标滑过菜单项时,会有一个背景遮罩层跟着鼠标移动,效果非常炫酷,图标还是矢量 ...

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

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

  3. GitHub上下载源代码的方法

    GitHub上找到自己要下载的项目以后,有3种方法可以下载源代码. 第一种是复制该项目的地址,然后用其他软件下载: 第二种是安装了官方客户端,可以直接点击"Clone in Desktop& ...

  4. How to return dictionary keys as a list in Python 3.3

    http://btmiller.com/2015/04/13/get-list-of-keys-from-dictionary-in-python-2-and-3.html Get a List of ...

  5. ahjesus配置vsftpd虚拟用户在Ubuntu

    网上搜索了很多资料,过时,不全,货不对版 已下步骤亲测有效,不包含匿名用户登录 1.新建/home/loguser.txt 并填充内容,格式如下 用户名密码用户名密码用户名密码 2.生成db文件用于用 ...

  6. linux信任公钥的配置

    一.每个用户都有自己的家目录 访问方式是:~/.ssh/id_rsa.pub 使用~就是表示家目录. 具体家目录在哪里,在用户密码配置文件中:/etc/passwd中.第6列的值就是. 可以使用~访问 ...

  7. MongoDB常用操作--数据库

    1.查看所有数据库,使用命令 show dbs 2.查看当前所在数据库,使用命令 db 3. 查看当前数据库中所有的集合,使用命令 show collections 或使用show tables 4. ...

  8. Heatmap.js v2.0 – 最强大的 Web 动态热图

    Heatmap 是用来呈现一定区域内的统计度量,最常见的网站访问热力图就是以特殊高亮的形式显示访客热衷的页面区域和访客所在的地理区域的图示.Heatmap.js 这个 JavaScript 库可以实现 ...

  9. Skytte:一款令人印象深刻的 HTML5 射击游戏

    Skytte 是一款浏览器里的 2D 射击游戏.使用 Canvas 元素和大量的 JavaScript 代码实现.Skytte 是用我们的开源和现代的前端技术创造的.经典,快节奏的横向滚动射击游戏,探 ...

  10. Gulp.js 参考手册,自动化构建利器

    Gulp 是最新的基于 Node 的自动化构建工具,希望能够取代 Grunt,成为最流行的 JavaScript 任务运行器.通过结合 NodeJS 的数据流的能力,只需几步就能搭建起自己的自动化项目 ...