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
      }
    });
  },

 

posted @ 2015-09-07 16:25  coding4范儿  阅读(103)  评论(0编辑  收藏  举报