无法访问JS对象中的值

时间:2014-09-03 11:12:09

标签: javascript

我想学习更多OO JavaScript。我创造了类似的东西:

    var Player = {
        player: $('#player'),
        video: Player.player
    };

    console.log(Player.video);

console.log返回Uncaught TypeError: Cannot read property 'player' of undefined。哪里是我的错?

2 个答案:

答案 0 :(得分:2)

初始化时无法引用对象。 找到解释和建议的解决方案here

显示你可能需要这样的东西:

var Player = new function() {
    this.player = $('#player');
    this.video = this.player;
}; 

console.log(Player.player);

答案 1 :(得分:0)

原因是在分配Player属性时尚未定义video

var Player = {
    player: $('#player'),
    video: Player.player  //<- Player is not defined yet. You are defining it right now.
};

创建对象后尝试设置video属性:

var Player = {
    player: $('#player')        
};
Player.video = Player.player;

console.log(Player.video);