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(); // 1

var ipod = new Gadget();

ipod.getLastId(); // 2

var ipad = new Gadget();

ipad.getLastId(); // 3

References: 

JavaScript Patterns - by Stoyan Stefanov (O`Reilly)

JavaScript Patterns 5.6 Static Members

时间: 2024-10-08 02:18:32

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 functionality to the prototype Parent.prototype.say = function () { return this.name; }; // empty child constructor function Child(name) {} inherit(Child, Paren

JavaScript Patterns 7.1 Singleton

7.1 Singleton The idea of the singleton pattern is to have only one instance of a specific class. This means that the second time you use the same class to create a new object, you should get the same object that was created the first time. var obj =

JavaScript Patterns 6.3 Klass

Commonalities • There’s a convention on how to name a method, which is to be considered the constructor of the class. • Classes inherit from other classes. • There’s access to the parent class (superclass) from within the child class. The function ta

JavaScript Patterns 5.3 Private Properties and Methods

All object members are public in JavaScript. var myobj = { myprop : 1, getProp : function() { return this.myprop; } }; console.log(myobj.myprop); // `myprop` is publicly accessible console.log(myobj.getProp()); // getProp() is public too The same is

JavaScript Patterns 3.2 Custom Constructor Functions

When you invoke the constructor function with new, the following happens inside the function: ? An empty object is created and referenced by this variable, inheriting the prototype of the function. ? Properties and methods are added to the object ref

JavaScript Patterns 5.7 Object Constants

Principle Make variables shouldn't be changed stand out using all caps. Add constants as static properties to the constructor function. // constructor var Widget = function () { // implementation... }; // constants Widget.MAX_HEIGHT = 320; Widget.MAX

JavaScript Patterns 5.4 Module Pattern

MYAPP.namespace('MYAPP.utilities.array'); MYAPP.utilities.array = (function () { // dependencies var uobj = MYAPP.utilities.object, ulang = MYAPP.utilities.lang, // private properties array_string = "[object Array]", ops = Object.prototype.toStr

JavaScript Patterns 3.7 Primitive Wrappers

Primitive value types: number, string, boolean, null, and undefined. // a primitive number var n = 100; console.log(typeof n); // "number" // a Number object var nobj = new Number(100); console.log(typeof nobj); // "object"  One reason

JavaScript Patterns 2 Essentials - Writing Maintainable Code

Revisiting the code after some time has passed requires: ? Time to relearn and understand the problem ? Time to understand the code that is supposed to solve the problem As the application matures, many other things happen that require your code to b