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. httpclient 调用WebAPI

    1.创建webapi项目,提供接口方法如下: /// <summary> /// 获取租户.位置下的所有传感器 /// </summary> /// <returns&g ...

  2. Xamarin.ios——First APP

    环境:MAC+Xamarin Studio 先讲讲安装吧,最普遍的方法就是去Xamarin官网,注册个账号,填写信息啥的开始下载,安装.但,在天朝的网络环境下,在下载android模块的东东时,总会下 ...

  3. java输出MYSQL数据库里面的数据最简单的实例

    import java.sql.*; public class JDBCExample { static final String JDBC_DRIVER = "com.mysql.jdbc ...

  4. 第 17 章 CSS 边框与背景[下]

    学习要点: 1.设置背景 主讲教师:李炎恢 本章主要探讨 HTML5 中 CSS 边框和背景,通过边框和背景的样式设置,给元素增加更丰富的外观. 一.设置背景 盒模型的尺寸可以通过两种方式实现可见性, ...

  5. 市面上常见的javaEE WEB服务软件

    常见的市面上web服务软件 Tomcat:轻量级的WEB应用程序服务器(开源),开源组织Apache的产品.免费的.支持部分的JavaEE规范.(servlet.jsp.jdbc,但ejb, rmi不 ...

  6. Verilog学习笔记认识提升篇(一)...............时序的基本概念(待补充)

    建立和保持时间: 建立时间(Tsu)是指在时钟上升沿到来之前数据必须保持稳定的时间,保持时间(Th)是指在时钟上升沿到来以后数据必须保持稳定的时间.一个数据需要在时钟的上升沿被锁存,那么这个数据就必须 ...

  7. Python的sorted函数应用

    sorted()函数也是一个高阶函数,它还可以接收一个key函数来实现自定义的排序 L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88) ...

  8. WinForm 窗体应用程序(初步)之一

    学习制作一个WinForm程序,有两样东西是需要首先掌握的.第一部分,我们称之为属性面板.无论是窗体还是控件,都有着自己的属性面板.第二部分,则是我们称之为控件的东西. 我们先来讨论一下属性面板.新建 ...

  9. win7系统下,vs2010一调式,vs就关闭要重启

    进入我的文档 %appdata%\Microsoft\VisualStudio, 将 10.0 重命名.网上找的方法有些问题,可能找这路径很难找到啊. 于是自己 找了找 一般都在当前用户文件夹下 Ap ...

  10. JavaScript基础19——innerHTML示例

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...