JavaScript Patterns 5.6 Static Members
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的更多相关文章
- JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance
// the parent constructor function Parent(name) { this.name = name || 'Adam'; } // adding functional ...
- 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 ...
- JavaScript Patterns 6.3 Klass
Commonalities • There’s a convention on how to name a method, which is to be considered the construc ...
- JavaScript Patterns 5.7 Object Constants
Principle Make variables shouldn't be changed stand out using all caps. Add constants as static prop ...
- JavaScript Patterns 5.3 Private Properties and Methods
All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return ...
- JavaScript Patterns 6.7 Borrowing Methods
Scenario You want to use just the methods you like, without inheriting all the other methods that yo ...
- JavaScript Patterns 6.6 Mix-ins
Loop through arguments and copy every property of every object passed to the function. And the resul ...
- JavaScript Patterns 6.5 Inheritance by Copying Properties
Shallow copy pattern function extend(parent, child) { var i; child = child || {}; for (i in parent) ...
- JavaScript Patterns 6.4 Prototypal Inheritance
No classes involved; Objects inherit from other objects. Use an empty temporary constructor function ...
随机推荐
- GCC、ARM-LINUX-GCC、ARM-ELF-GCC浅析
一.GCC简介: The GNU Compiler Collection,通常简称GCC,是一套由GNU开发的编译器集,为什么是编辑器集而不是编译器呢?那是因为它不仅支持C语言编译,还支持C++, A ...
- MVC之前的那点事儿系列(2):HttpRuntime详解分析(上)
文章内容 从上章文章都知道,asp.net是运行在HttpRuntime里的,但是从CLR如何进入HttpRuntime的,可能大家都不太清晰.本章节就是通过深入分析.Net4的源码来展示其中的重要步 ...
- jquery常见知识点 总结
1. jquery特点 2. jquery中css选择器用法 jQuery使用了一套css选择器,共有5种,即标签选择器,ID选择器,类选择器,通用选择器和群组选择器,现分述如下: 标签选择器 用 ...
- Node.JS文件系统解析
1.Node.js 文件系统 var fs = require("fs") 2.异步和同步 读取文件内容的函数有异步的 fs.readFile() 和同步的 fs.readFile ...
- windows 7/10下安装oracle 10g
有段时间没搞oracle了,最近要给别人在win 7下装个oracle 10g,特记录备忘下. 使用http://download.oracle.com/otn/nt/oracle10g/10201/ ...
- [TypeScript] Dictionary范例
[TypeScript] Dictionary范例 Playground http://tinyurl.com/o7czcxo Samples class Dictionary { [index: s ...
- WPF如何实现一款类似360安全卫士界面的程序?(共享源码!)
以前学习Windows Form编程的时候,总感觉自己做的界面很丑,看到360安全卫士.迅雷等软件的UI设计都非常美观,心里总是憧憬着要是自己能实现这样的UI效果该多好!!!另一个困扰我的问题是,这个 ...
- NodeBB – 基于 Node.js 的开源论坛系统
NodeBB 是一个更好的论坛平台,专门为现代网络打造.它是免费的,易于使用. NodeBB 论坛软件是基于 Node.js 开发,支持 Redis 或 MongoDB 的数据库.它利用 Web So ...
- jquery easyui tabs单击刷新右键刷新
单击刷新 $(".tabs-inner").click(function(){var currTab = self.parent.$('#tabs').tabs('getSelec ...
- [原创]html5游戏_贪吃蛇
代码随便写写,尚有许多不足,PC与手机端皆可运行 手机端滑屏操作,PC端方向键操作 疑问: 生成食物,与判断是否可以移动方面 有两种实现方式, 1.使用js内存,数组循环判断 2.使用dom的quer ...