Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/479.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 写JS对象中的奇怪部分_Javascript - Fatal编程技术网

Javascript 写JS对象中的奇怪部分

Javascript 写JS对象中的奇怪部分,javascript,Javascript,我对javascript非常陌生。我使用codepen.io学习一些javascript项目。我找到了PongJS实现。我发现了一些我无法理解的东西。因为我不知道这是什么东西 var Game = { initialize: function () { this.canvas = document.querySelector('canvas'); this.context = this.canvas.getContext('2d'); this.canvas.width

我对javascript非常陌生。我使用codepen.io学习一些javascript项目。我找到了PongJS实现。我发现了一些我无法理解的东西。因为我不知道这是什么东西

var Game = {
initialize: function () {
    this.canvas = document.querySelector('canvas');
    this.context = this.canvas.getContext('2d');

    this.canvas.width = 1400;
    this.canvas.height = 1000;

    this.canvas.style.width = (this.canvas.width / 2) + 'px';
    this.canvas.style.height = (this.canvas.height / 2) + 'px';

    this.player = Paddle.new.call(this, 'left');
    this.paddle = Paddle.new.call(this, 'right');
    this.ball = Ball.new.call(this);

    this.paddle.speed = 8;
    this.running = this.over = false;
    this.turn = this.paddle;
    this.timer = this.round = 0;
    this.color = '#2c3e50';

    Pong.menu();
    Pong.listen();
}, //there are another properties
我的问题是为什么它使用
这个。
来生成一些变量。为什么不使用
var
?因为我发现了
这个语法,我无法将这些解释联系起来。
我希望你能帮助我理解这行代码


这是代码笔:

这是指当前上下文。在本例中,上下文是游戏对象。所以当你这样做的时候:

Game.initialize();
这是指游戏,因此所有内容都存储在游戏中,例如:

Game.canvas
这是非常有用的,因为你的数据是正确的,如果它是相关的,那么我们不知道它属于哪一个变量画布,而是知道Game.canvas与Game相关。另一件很酷的事情是,您可以轻松创建新游戏:

var pong = Object.create( Game );
pong.initialize();
console.log( pong.canvas );

若我并没有错,我必须引用对象来存储数据。那么当您使用var时?谢谢你的简短解释,先生。这是一个很好的例子:)@hasby fahrudin只需使用变量(函数内部)来存储函数内部可访问的值,这些值可以在以后轻松删除。