观察者课程,它应该做什么以及应该做什么

时间:2014-09-26 02:09:58

标签: javascript class

我正在尝试学习更多Javascript,所以我正在阅读一些书籍并制作一些练习来练习我正在学习的内容。其中一个练习说如下:

1 - 创建一个Movie对象:

电影

-attributes:hashmap

  • play()

  • 停止()

  • set(attr:string,value)

  • 的get(ATTR:字符串)

2-添加一个侦听“播放”和“已停止”事件的MovieObserver类。

我做了以下事情:

function Movie(){
this.attributes = {
    title : 'undefined',
    duration : '0',
    director : 'undefined',
    actor : []
}
}

Movie.prototype.set(attr , value){
    this.attributes[attr] = value;
}

Movie.prototype.get(){
    console.log(this.attributes['title']);
    return this.attributes['title'];
}

Movie.prototype.play(){
    console.log ('Playing '+this.attributes['title']+'...');
}

Movie.prototype.stop(){
    console.log ('Stopped '+this.attributes['title']+'...');
}

我需要知道的是MovieObserver应该做什么,我无法理解。

1 个答案:

答案 0 :(得分:0)

真的只是评论。

  

所以我认为没关系,

不,不是。在构造函数中,属性对象未分配给实例(当使用 new 时,该对象被分配给构造函数&#39> em>),所以你可能想要(使用Spencer的修复程序):

function Movie(){
  this.attributes = {
    'title' : 'undefined',
    'duration' : '0',
    'director' : 'undefined',
    'actor' : []
  }
}

现在你做的时候:

var movie = new Movie();

console.log(movie.attributes.duration) // 0
相关问题