我只是从Javascript开始,我不确定是否已经为对象类型声明正确声明了这些。是否有更好的方法和更易读的方法将变量声明为Javascript中的类,或者这是最简单的方法。
从我阅读的内容来看,blueObject是公正而空的对象。这是在Javascript中创建简单对象类型的唯一方法,还是有更好,更易读的方法?
码:
var blueObject={}; //Creates and object
var redObject={}; //Creates and object
blueObject.x=0;
blueObject.y=200;
blueObject.dx=2;
blueObject.width=48;
blueObject.height=48;
blueObject.image=new Image();
blueObject.image.src="blueplus.png";
context.drawImage(blueObject.image, 0, 0);
blueObject.blueImageData=context.getImageData(0, 0, blueObject.width,
blueObject.height);
context.clearRect(0,0,theCanvas.width, theCanvas.height);
redObject.x=348;
redObject.y=200;
redObject.dx=-2;
redObject.width=48;
redObject.height=48;
redObject.image=new Image();
redObject.image.src="redcircle.png";
这可能是您达到目的的最干净的方法。
var blueObject = {
x: 0,
y: 200,
dx: 2,
width: 48,
height: 48,
image: new Image()
};
var redObject = {
x: 348,
y: 200,
dx: -2,
width: 48,
height: 48,
image: new Image();
};
blueObject.image.src = "blueplus.png";
redObject.image.src = "redCircle.png";
context.drawImage(blueObject.image, 0, 0);
blueObject.blueImageData = context.getImageData(0, 0, blueObject.width,
blueObject.height);
context.clearRect(0, 0, theCanvas.width, theCanvas.height);
另请参阅此答案,以了解有关创建JavaScript对象的更正式方法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句