js 面向对象

149 阅读1分钟
function Producer() {
    // 这个 if 只是避免使用者不小心把 Producer 当作函式来调用
    if(!(this instanceof Producer)) {
      throw new Error('请用 new Producer()!');
      // 仿 ES6 行为可用: throw new Error('Class constructor Producer cannot be invoked without 'new'')
    }
}