我想学习更多OO JavaScript。我创造了类似的东西:
var Player = {
player: $('#player'),
video: Player.player
};
console.log(Player.video);
console.log
返回Uncaught TypeError: Cannot read property 'player' of undefined
。哪里是我的错?
答案 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);