关于变量的简单Javascript
我正在学习一个教程,他在教程中声明了变量,然后继续用它们做一些我不理解的事情关于变量的简单Javascript,javascript,html,variables,2d-games,Javascript,Html,Variables,2d Games,我正在学习一个教程,他在教程中声明了变量,然后继续用它们做一些我不理解的事情 var player, ai, ball; player = { // this is the code I am referring to x: null, y: null, width: 20, height: 100, update: function(){}, draw: function(){ ctx.fillRect(this.x, thi
var player, ai, ball;
player = { // this is the code I am referring to
x: null,
y: null,
width: 20,
height: 100,
update: function(){},
draw: function(){
ctx.fillRect(this.x, this.y, this.width, this.height);
}
};
他是在变量中添加变量吗?
谢谢。是的,它正在声明一个javascript对象 你可能需要一些 对象也是变量。但是对象可以包含许多值
此代码为变量指定一个对象。每个冒号左侧的字符串是属性名。代码为属性赋值,最后两个属性指定要调用的函数 属性可以这样调用:
var playerAge = player.age;
类似地,这些函数将被称为:
player.draw();
不仅仅是在变量中设置变量。他将
player
设置为一个Javascript对象。此对象包含像x
和y
这样的成员,以及函数update
和draw
感谢您的帮助,简单介绍一下成员(或元素)是由Javascript定义的还是我可以称之为MyX,MyY?从技术上讲,这些不是成员。通常的用法是将它们称为属性。谢谢你的帮助我现在可以得到它。谢谢你的帮助我现在可以得到它。非常感谢你的帮助我现在可以得到它。我不知道你所说的
这个对象有var update
是什么意思update
在这里不是一个变量,它是一个属性,这是一个非常不同的东西。
var player, ai, ball; // this declare variables
player = { // player = {} , it means player is a object
x: null, // this object have a var call x;
y: null,
width: 20,
height: 100,
// this object has a property "update", and update is a function;
update: function(){},
// this object has a property "draw", and draw is a function;
draw: function(){
ctx.fillRect(this.x, this.y, this.width, this.height);
}
};