简体   繁体   English

与PHP相比,我如何看待JavaScript中的成员变量

[英]How do I think about member variable in JavaScript in comparison to PHP

I'm playing around with this JavaScript code that is used for a game in a HTML5 canvas element (in order to learn js). 我正在玩这个JavaScript代码,该代码用于HTML5 canvas元素中的游戏(为了学习js)。 I have 3 questions I struggle to find answers relating to a "game' object which has some methods (see bellow). 我有3个问题,我很难找到与具有某些方法的“游戏”对象有关的答案(请参见下面的内容)。

  1. In the "load" method "this" is used to set some member variables and to call on its own methods, but then in some of the other methods 'game' is used instead. 在“加载”方法中,“ this”用于设置一些成员变量并调用自己的方法,但是在其他一些方法中,则使用“游戏”代替。 Is there some good reasons for not using 'this' in these other methods as well (because they are both doing the same thing right)? 还有其他一些理由也不要在其他方法中不使用“ this”(因为它们都在做同样的事情)吗? (I've found an answer that say it can be done to make the code more clear in case the risk of changing the variable name is low, but why wouldn't "game" be used in both places then?) (我找到了一个答案,说可以在更改变量名称的风险较低的情况下使代码更加清晰,但是为什么不在两个地方都使用“游戏”?)

  2. I'm also a bit confused about the variables. 我对变量也有些困惑。 Does both"this.audioPath = 'audio/';" 是否都执行“ this.audioPath ='audio /';” and "Game.height = height;" 和“ Game.height =高度;” set a member variable for the "game" object, and are they public or private? 设置“游戏”对象的成员变量,它们是公共的还是私有的?

  3. "Game.input = {...}" and "" Game.frames = {}" are included as separate js files after the "game" file. but they are referenced in the "load" method of the "game" object ie "this.input.init();" and "this.frames.init();". I assume this means that they are two objects which are member variables of the "game" object, but don't they have to be initiated before you can use there methods? “ Game.input = {......”和““ Game.frames = {}”作为“ js”文件包含在“ game”文件之后,但在“ game”对象的“ load”方法中被引用即“ this.input.init();”和“ this.frames.init();”。我认为这意味着它们是两个对象,它们是“游戏”对象的成员变量,但不必在可以使用那里的方法之前被初始化?

I'm used to thinking in terms of PHP (classes) so this is all new and very confusing to me. 我习惯于用PHP(类)来思考,所以这对我来说是全新的,而且非常令人困惑。 I have been searching for answers but I find it very hard to know what to search for... 我一直在寻找答案,但是我很难知道要寻找什么...

var Game = {
load: function(game) {
    // this.debug = true;
    this.audioPath = 'audio/';
    this.createCanvas(1200, 675);
    this.initGlobalVariables();
    this.loadedGame = game;
    this.loadScene('initial');
    this.input.init();
    this.frames.init();
    this.frames.play();
},

clearCanvas: function() {
    Game.ctx.clearRect(0, 0, Game.canvas.width, Game.canvas.height);
},

createCanvas: function(width, height) {
    Game.height = height;
    Game.width = width;
    Game.canvas = document.createElement('canvas');
    Game.ctx = Game.canvas.getContext('2d');
    Game.canvas.width = width;
    Game.canvas.height = height;
    document.getElementById('canvas-wrapper').appendChild(Game.canvas);
},

getRandomNumber: function(min, max) {
    return Math.floor(Math.random() * (max - min + 1)) + min;
},

initGlobalVariables: function() {
    Game.loadedGame = {};
    Game.keysDown = [];
},

isCollision: function(a, b){
    return  a.x <= (b.x + b.width) &&
            b.x <= (a.x + a.width) &&
            a.y <= (b.y + b.height) &&
            b.y <= (a.y + a.height);
},

loadScene: function(scenes) {
    Game.scene = Game.loadedGame[scenes];
    Game.scene.init();
}
};

Is there some good reasons for not using 'this' in these other methods as well (because they are both doing the same thing right)? 还有其他一些理由也不要在其他方法中不使用“ this”(因为它们都在做同样的事情)吗?

this in javascript does not always point to the same object, but depends on how the function is called. this在javascript中并不总是指向相同的对象,而是取决于函数的调用方式。 Game does in contrast. 相反, Game确实如此。 This is most relevant when functions are passed as callbacks . 当函数作为回调传递时,这是最相关的。

I'm also a bit confused about the variables. 我对变量也有些困惑。 Does both"this.audioPath = 'audio/';" 是否都执行“ this.audioPath ='audio /';” and "Game.height = height;" 和“ Game.height =高度;” set a member variable for the "game" object, and are they public or private? 设置“游戏”对象的成员变量,它们是公共的还是私有的?

Yes, both set properties of the Game object. 是的,都设置了Game对象的属性。 Properties in JavaScript are always public. JavaScript中的属性始终是公共的。

"Game.input = {...}" and "" Game.frames = {}" are included as separate js files after the "game" file. but they are referenced in the "load" method of the "game" object ie "this.input.init();" and "this.frames.init();". I assume this means that they are two objects which are member variables of the "game" object, but don't they have to be initiated before you can use there methods? “ Game.input = {......”和““ Game.frames = {}”作为“ js”文件包含在“ game”文件之后,但在“ game”对象的“ load”方法中被引用即“ this.input.init();”和“ this.frames.init();”。我认为这意味着它们是两个对象,它们是“游戏”对象的成员变量,但不必在可以使用那里的方法之前被初始化?

Yes, they are initialised before their methods are used. 是的,它们在使用方法之前初始化。

1: In your case there is no difference, as you only have one single instance of the object, and you have a variable that references that instance. 1:在您的情况下没有区别,因为您只有一个对象的单个实例,并且您有一个引用该实例的变量。 If it would be possible to have multiple instances, then you would need to use this to reference the current instance for the code to be usable for all instances. 如果可能有多个实例,则您需要使用this来引用当前实例,以使代码可用于所有实例。 Also, even if you have a single instance, it doesn't have to store it in a plain variable. 同样,即使您只有一个实例,也不必将其存储在一个普通变量中。 It could for example be a property of another object, or an item in an array. 例如,它可以是另一个对象的属性,也可以是数组中的项目。

2: Yes, they do set member variables. 2:是的,它们确实设置了成员变量。 Member variables are always public, there is no concept of private members in Javascript. 成员变量始终是公共的,在Javascript中没有私有成员的概念。 To have anything that is private, you would wrap code in a function so that you could declare variables that are private to that scope. 要拥有私有的内容,您可以将代码包装在一个函数中,以便可以声明该作用域私有的变量。 There is no object scope in Javascript, only global scope and function scope. Javascript中没有对象范围,只有全局范围和函数范围。

3: Yes, the properties have to be assigned before you can call methods on them. 3:是的,必须先分配属性,然后才能在其上调用方法。 The Game.load method thus have to be called after the properties are added to the object. 因此,在将属性添加到对象之后,必须调用Game.load方法。 The load method is just an ordinary method, it's not like a constructor that is called when the object is created, so there will be a call to it somewhere in the page. load方法只是一个普通方法,它与创建对象时调用的构造函数不同,因此将在页面中的某个位置对其进行调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM