@@ -853,7 +853,7 @@ function bind (object, method) {
853
853
* ES5 has bind builtin, it also accepts partial argument list
854
854
855
855
```js
856
- //basic implementation handling partial application
856
+ //basic example implementation handling partial application
857
857
Function.prototype.bind = Function.prototype.bind || function(thisArg) {
858
858
var fn = this;
859
859
var slice = Array.prototype.slice;
@@ -863,4 +863,37 @@ Function.prototype.bind = Function.prototype.bind || function(thisArg) {
863
863
return fn.call(thisArg, args.concat(slice.call(arguments)))
864
864
}
865
865
}
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