为什么这个JavaScript OO错了?

我将以简单的方式放置我的代码,但如果有任何好的灵魂希望帮助代码是在Github上/lib/inmotion/editor.js(line 99) editor.js内
function Editor(){
  var instance = this;
  this.key_frames = [] // a list of key frames
  this.kf = null   // the current KeyFrame

  function notice_click( click ){
    instance.kf.add_bone( 1 ) // <-- error here
    // instance.kf.list.push( 1 ) // <-- this works, but is incomplete
  }
}
key_frame.js
function KeyFrame(){
  instance = this
  instance.changed = false // for the redraw :)
  instance.list = []

  function add_bone( bone ){
    instance.list.push( bone )
    instance.changed = true
    return true
  }
}
我的程序有一个唯一的Editor实例,它有很多KeyFrames实例。 每个KeyFrame都有很多骨头。 所以总有一个活动的KeyFrame,由Editor.kf实例定义 由于我只有一个kf,我可以毫无问题地为那个添加骨骼,但是当我添加更多KeyFrames时,我只能将add_bone添加到我创建的最后一个kf中!为什么? 如果我不够清楚,我道歉,怀疑     
已邀请:
你的
KeyFrame
对象缺少
var
,它应该如下所示:
function KeyFrame(){
  var instance = this;
  instance.changed = false;
  instance.list = [];

  function add_bone( bone ){
    instance.list.push( bone );
    instance.changed = true;
    return true;
  }
}
没有
var
关键字你定义或覆盖
window.instance
,一个全局变量,而不是你想要的对象的本地变量:)     
据我所知,问题是每次调用KeyFrame时“实例”都会被覆盖。你应该直接使用'this'。     

要回复问题请先登录注册