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. C#DateTimePicker控件问题

    DateTimPicker控件在遇到29这样特殊的日期,选择时可能会出现 解决方案:在属性中把Value值设置为除29日外的其他日期或者在代码中直接设置Value值:DateTimePicker1 = ...

  2. 【Java每日一题】20161103

    package Nov2016; import java.util.List; public class Ques1103 { public void method01(String[] array) ...

  3. 【Linux_Fedora_应用系列】_4_安装chrome浏览器

    在前面一篇文章中,我们讨论了在Linux Fedora 14下安装WMV解码器:[Linux_Fedora_应用系列]_3_如何利用Smplayer播放WMV格式的文件 在文章中介绍的方法同样适合FC ...

  4. Thinkphp 用PHPExcel 导入Excel

    搞了个简单的Excel导入, 用的是PHPExcel(百科:用来操作Office Excel文档的一个PHP类库, 基于微软的OpenXML标准和PHP语言) 好, 不说了, 开始吧... 首先得有P ...

  5. 成为 Web 开发大师你必须知道的 7 件事情

    曾经是这样的,懂点编码,并可以偶尔耍点酷,那么你就会被认为是一个Web开发大师.但是现在,情况再也不是这样的了.Web开发已经朝着主流方向发展,开发人员数量显著增加.这意味着,如果你想成为这个领域的大 ...

  6. 带给您灵感的25个最新鲜的 HTML5 网站

    感谢 HTML5 带来的惊人的先进特性,在未来几年,HTML5 将会继续发挥巨大的推动作用,不仅是在 Web 应用中,网页设计领域也会有新的变革.今天,我们在这里集合了能够带给您灵感的25个最新鲜的 ...

  7. 【html5】Web存储_locaStorage对象的应用

    Web存储 html5可以在本地存储用户浏览的数据,数据的存储原理是以 键/值 存储的 存储对象分类 localStorage:没有时间限制的数据存储 sessionStorage:针对一个会话的数据 ...

  8. Web持久化存储Web SQL、Local Storage、Cookies(常用)

    在浏览器客户端记录一些信息,有三种常用的Web数据持久化存储的方式,分别是Web SQL.Local Storage.Cookies. Web SQL 作为html5本地数据库,可通过一套API来操纵 ...

  9. js动态计算移动端rem

    在做移动端web app的时候,众所周知,移动设备分辨率五花八门,虽然我们可以通过CSS3的media query来实现适配,例如下面这样: html { font-size : 20px; } @m ...

  10. JavaScript技巧[转载]

    在这篇文章中将给大家分享12个有关于JavaScript的小技巧.这些小技巧可能在你的实际工作中或许能帮助你解决一些问题. 使用!!操作符转换布尔值 有时候我们需要对一个变量查检其是否存在或者检查值是 ...