JavaScript编程珠玑
1. 安全的创建对象
Function Obj(name) { if (!(this instanceof Obj)) { return new Obj(name); } this.name = name; } // both is right var obj1 = Obj(); var obj2 = new Obj();
2. 继承最佳实践
inherits: function(subCtor, superCtor) { subCtor.super_ = superCtor; subCtor.prototype = Object.create(superCtor.prototype, { constructor: { value: subCtor, enumerable: false, writable: true, configurable: true } }); },