惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
C
Check Point Blog
博客园_首页
MongoDB | Blog
MongoDB | Blog
N
Netflix TechBlog - Medium
F
Full Disclosure
Microsoft Security Blog
Microsoft Security Blog
爱范儿
爱范儿
Recent Announcements
Recent Announcements
阮一峰的网络日志
阮一峰的网络日志
G
GRAHAM CLULEY
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
V
Vulnerabilities – Threatpost
K
Kaspersky official blog
博客园 - 司徒正美
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
Project Zero
Project Zero
云风的 BLOG
云风的 BLOG
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
V
V2EX - 技术
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Spread Privacy
Spread Privacy
罗磊的独立博客
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
S
Security Affairs
SecWiki News
SecWiki News
Schneier on Security
Schneier on Security
O
OpenAI News
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Cyberwarzone
Cyberwarzone
Y
Y Combinator Blog
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
I
InfoQ
D
Docker
P
Palo Alto Networks Blog
Recorded Future
Recorded Future
M
MIT News - Artificial intelligence
博客园 - Franky
B
Blog
Scott Helme
Scott Helme
博客园 - 叶小钗
D
DataBreaches.Net

博客园 - 小郝(Kaibo Hao)

Big O Complexity Chart (From: bigocheatsheet.com) Machine Learning Note - Note 1 JavaScript Patterns 7.1 Singleton JavaScript Patterns 6.7 Borrowing Methods JavaScript Patterns 6.6 Mix-ins JavaScript Patterns 6.5 Inheritance by Copying Properties JavaScript Patterns 6.4 Prototypal Inheritance JavaScript Patterns 6.3 Klass JavaScript Patterns 6.2 Expected Outcome When Using Classical Inheritance JavaScript Patterns 6.1 Classical Versus Modern Inheritance Patterns JavaScript Patterns 5.9 method() Method JavaScript Patterns 5.8 Chaining Pattern JavaScript Patterns 5.7 Object Constants JavaScript Patterns 5.5 Sandbox Pattern JavaScript Patterns 5.4 Module Pattern JavaScript Patterns 5.3 Private Properties and Methods JavaScript Patterns 5.2 Declaring Dependencies JavaScript Patterns 5.1 Namespace Pattern JavaScript Patterns 4.10 Curry
JavaScript Patterns 5.6 Static Members
小郝(Kaibo Hao) · 2014-06-30 · via 博客园 - 小郝(Kaibo Hao)

2014-06-30 22:52  小郝(Kaibo Hao)  阅读(367)  评论()    收藏  举报

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)