00 How it works
01 Objects - Constructor
02 Objects - Constructor (drawing)
03 Objects - Constructor (timing)
04 Objects - Prototype (sequential declaration)
05 Objects - Prototype (declaring all at once)
Code goes here:
var _things = 0; function Thing(angle) { // Variables declared in constructor are private to the constuctor. var thing_num = ++_things; // Variables accessed trhough 'this' keyword are public and visible everywhere. this.x = 0; this.y = 0; this.alpha = angle ? (angle / 180.0*Math.PI) : 0; // Class methods follow this.step = function(distance) { this.x += distance * Math.cos(this.alpha); this.y += distance * Math.sin(this.alpha); return this; }; this.update = function(canvas, domelement) { console.log("Thing #" + thing_num + " is at:", it01.x, 'x', it01.y, "and looks at", this.alpha * 180/Math.PI, "degrees"); return this; } } // Create a new Object from function Thing(angle) var it01 = new Thing(66); // Play with it! it01.update() .step(10).update() .step(5).update();
Ctrl+Enter to Run this code
Logs show up here: