February 2012
5 posts
1 tag
imagination
Updated.
var f = function() {
var __kind = "O";
return function(name) {
// private things here
var _vars = {};
_vars["kind"] = function() {
return __kind;
};
_vars["name"] = name || "anonymous";
// then return a new instance
// this should be hide by rewriting
return {
get: function(key) {
...
2 tags
Fun with JavaScript Singleton
var Normal = function() {
//initialize here
this.x = 1;
this.y = 2;
};
var Singleton = function() {
var shared = function() {
//initialize here
this.x = 10;
this.y = 20;
};
shared.meow = function() {
console.log("meow!");
};
return function() {
return shared;
};
}();
/* show time! */
var a0 = new Normal();
var a1...
viewDidLoad
…This method is called regardless of whether the view...
– Scumbag UIViewController Class Reference
Cocoa Design Patterns Errata > creating the... →
跟 Objective-C還有 Cocoa很不熟, Cocoa Design Patterns還真是開了我的眼界。
可是 Singleton的 code不能用了。
原來是因為 [super alloc]會叫到 [self allocWithZone:]。文中提到的辦法是改叫 [super allocWithZone:nil]來閃過,總覺得不是那麼直觀,不過也只好將就用用先。
P.s. 還好沒看中文版,那句 One pattern to rule them all的味道沒了XD。
3 tags
prototype的聯想
去年中開始看 Douglas Crockford的 “JavaScript: The Good Parts”,才知道 JavaScript這幾年不是無緣無故翻身。而 prototype-based OOP與 class-based OOP的比較,讓我對 OOP有了不同的認識。
先看看 prototype繼承的用法, Crockford的作法是:
function beget(o) {
function F() {};
F.prototype = o;
return new F();
}
之所以不能簡單的先隨便弄個新的 instance n再讓 n.prototype = o,是因為 ECMAScript不云許隨便抽換 instance的 prototype,真正的 prototype是存在一個內部變數, Dmitry Soshnikov稱它為...