Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fbdee2c

Browse files
singleton
1 parent 32623ce commit fbdee2c

File tree

1 file changed

+35
-2
lines changed

1 file changed

+35
-2
lines changed

‎readme.md

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ function bind (object, method) {
853853
* ES5 has bind builtin, it also accepts partial argument list
854854
855855
```js
856-
//basic implementation handling partial application
856+
//basic example implementation handling partial application
857857
Function.prototype.bind = Function.prototype.bind || function(thisArg) {
858858
var fn = this;
859859
var slice = Array.prototype.slice;
@@ -863,4 +863,37 @@ Function.prototype.bind = Function.prototype.bind || function(thisArg) {
863863
return fn.call(thisArg, args.concat(slice.call(arguments)))
864864
}
865865
}
866-
````
866+
```
867+
## 7. Design patterns
868+
869+
* as made famous by the GoF book
870+
* language independent but mainly for Java/C++ like strongly typed languages
871+
* most of them are really easy to implement in JavaScript
872+
873+
### singleton
874+
875+
* only one instance of specific class
876+
* JS: no classes, when you create a new object there's no other like it
877+
* (in JS some people mean Chapter 5. module pattern by singletons)
878+
* you might want singletons when using ```new```:
879+
* store instance as a (static) property on constructor function
880+
* but static property is public
881+
* can also protect instance as a private static member (via closure)
882+
* (be careful not to wipe out prototype properties)
883+
884+
```js
885+
var IAmSingleton;
886+
887+
(function(){
888+
var instance;
889+
IAmSingleton = function() {
890+
if (instance) {
891+
return instance;
892+
}
893+
instance = this;
894+
895+
//... all the functionality
896+
this.whatever = true;
897+
}
898+
})();
899+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /