読者です 読者をやめる 読者になる 読者になる

by shigemk2

当面は技術的なことしか書かない

Foundation HTML5 Canvas: For Games and Entertainment Object

JavaScript

オブジェクトをロケットにたとえて説明している。
ロケット本体がオブジェクト、ロケットの機能がメソッドであり、
エンジンの数、ロケットの色などを、メソッドを利用して修正する。

The best way to think of objects in JavaScript is to imagine them like real-world objects, like a car, or a rocket – I like rockets. An object in JavaScript has properties and methods, as does our real-world rocket object.

If we take this example further, the properties of a rocket would be things like the number of engines it has, the amount of thrust it has, the number of astronauts on board, or even something as simple as its color. A property is essentially a value that describes something about the object. On the other hand, the methods of a rocket would be things like turning the engines on and off, opening the door for the astronauts, or sending a message to ground control. A method is an action of some sort that is performed either on or by the object at hand.

var rocket = new Object();
rocket.engineCount = 2;
rocket.thrust = 5000;
rocket.astronautCount = 4;
rocket.colour = "red";
var rocket = new Object();
rocket.engineCount = 2;
var anotherRocket = new Object();
anotherRocket.engineCount = 1;
anotherRocket.wings = 2;

Both rocket objects are seemingly identical, apart from the second rocket has a new property called wings. Because the first rocket doesn't have this, we encounter a pretty killer problem.

function Rocket(engineCount, thrust, wings) {
   this.engineCount = engineCount;
   this.thrust = thrust;
   this.wings = wings;
};

the document object also provides a few methods that allow you to access specific elements, based on their attributes or tag name.