Public Static Members

  1. // constructor
  2.  
  3. var Gadget = function (price) {
  4.  
  5. this.price = price;
  6.  
  7. };
  8.  
  9. // a static method
  10.  
  11. Gadget.isShiny = function () {
  12.  
  13. // this always works
  14.  
  15. var msg = "you bet";
  16.  
  17. // Checking if the static method is called by instance.
  18. if (this instanceof Gadget) {
  19. // this only works if called non-statically
  20.  
  21. msg += ", it costs $" + this.price + '!';
  22.  
  23. }
  24. return msg;
  25.  
  26. };
  27.  
  28. // a normal method added to the prototype
  29.  
  30. Gadget.prototype.setPrice = function (price) {
  31.  
  32. this.price = price;
  33.  
  34. };
  35.  
  36. // a normal method added to the prototype
  37.  
  38. Gadget.prototype.isShiny = function () {
  39.  
  40. return Gadget.isShiny.call(this);
  41.  
  42. };
  43.  
  44. // Attempting to call an instance method statically won’t work
  45.  
  46. typeof Gadget.setPrice; // "undefined"

Testing a static method call:

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

Testing an instance, nonstatic call:

  1. var a = new Gadget('499.99');
  2.  
  3. 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

  1. // constructor
  2.  
  3. var Gadget = (function () {
  4.  
  5. // static variable/property
  6.  
  7. var counter = 0,
  8.  
  9. NewGadget;
  10.  
  11. // this will become the new constructor implementation

  12. NewGadget = function () {
  13. counter += 1;
  14. };
  15. // a privileged method
  16.  
  17. NewGadget.prototype.getLastId = function () {
  18.  
  19. return counter;
  20.  
  21. };
  22.  
  23. // overwrite the constructor
  24.  
  25. return NewGadget;
  26.  
  27. }()); // execute immediately
  28.  
  29. var iphone = new Gadget();
  30.  
  31. iphone.getLastId(); //
  32.  
  33. var ipod = new Gadget();
  34.  
  35. ipod.getLastId(); //
  36.  
  37. var ipad = new Gadget();
  38.  
  39. 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. GCC、ARM-LINUX-GCC、ARM-ELF-GCC浅析

    一.GCC简介: The GNU Compiler Collection,通常简称GCC,是一套由GNU开发的编译器集,为什么是编辑器集而不是编译器呢?那是因为它不仅支持C语言编译,还支持C++, A ...

  2. MVC之前的那点事儿系列(2):HttpRuntime详解分析(上)

    文章内容 从上章文章都知道,asp.net是运行在HttpRuntime里的,但是从CLR如何进入HttpRuntime的,可能大家都不太清晰.本章节就是通过深入分析.Net4的源码来展示其中的重要步 ...

  3. jquery常见知识点 总结

    1. jquery特点 2. jquery中css选择器用法 jQuery使用了一套css选择器,共有5种,即标签选择器,ID选择器,类选择器,通用选择器和群组选择器,现分述如下:   标签选择器 用 ...

  4. Node.JS文件系统解析

    1.Node.js 文件系统 var fs = require("fs") 2.异步和同步 读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFile ...

  5. windows 7/10下安装oracle 10g

    有段时间没搞oracle了,最近要给别人在win 7下装个oracle 10g,特记录备忘下. 使用http://download.oracle.com/otn/nt/oracle10g/10201/ ...

  6. [TypeScript] Dictionary范例

    [TypeScript] Dictionary范例 Playground http://tinyurl.com/o7czcxo Samples class Dictionary { [index: s ...

  7. WPF如何实现一款类似360安全卫士界面的程序?(共享源码!)

    以前学习Windows Form编程的时候,总感觉自己做的界面很丑,看到360安全卫士.迅雷等软件的UI设计都非常美观,心里总是憧憬着要是自己能实现这样的UI效果该多好!!!另一个困扰我的问题是,这个 ...

  8. NodeBB – 基于 Node.js 的开源论坛系统

    NodeBB 是一个更好的论坛平台,专门为现代网络打造.它是免费的,易于使用. NodeBB 论坛软件是基于 Node.js 开发,支持 Redis 或 MongoDB 的数据库.它利用 Web So ...

  9. jquery easyui tabs单击刷新右键刷新

    单击刷新 $(".tabs-inner").click(function(){var currTab = self.parent.$('#tabs').tabs('getSelec ...

  10. [原创]html5游戏_贪吃蛇

    代码随便写写,尚有许多不足,PC与手机端皆可运行 手机端滑屏操作,PC端方向键操作 疑问: 生成食物,与判断是否可以移动方面 有两种实现方式, 1.使用js内存,数组循环判断 2.使用dom的quer ...