对象关联代码风格

  1. Task = {
  2. setID: function(ID){this.id = ID;}
  3. outputID: function(){console.log(this.id);}
  4. };
  5. XYZ = Object.create(Task);
  6. XYZ.prepareTask = function(ID,Label){
  7. this.setID(ID);
  8. this.label = Label;
  9. }
  10. XYZ.outputTaskDetails = function(){
  11. this.outputID();
  12. console.log(this.label);
  13. }
  1. 最好将状态保存在委托者内。例如 idlabel 数据成员直接储存在 XYZ 上。
  2. 委托行为中,尽量避免在原型链的不同级别使用相同的命名
  3. this.setID(ID); 调用时 setID(ID) 会使用原型链找到方法,由于调用位置触发了 this 的隐性绑定规则,虽然 setID(ID) 方法在 Task 中,this 运行时仍然会绑定到 XYZ

对象中的函数和对象并没有作用域包含关系,更没有原型链的关系