JavaScript is an interpreted Programming Language that was originally implemented as part of Web browsers in order that client-side scripts can interact with the user, control the browser, communicate asynchronously and alter document Content that is displayed.
In JavaScript Syntax variables are declared like RenderEngineClass below. Variables are defined by starting with the keyword var.
// Declaring a [JavaScript](/blog/530/) object using literal notation
var RenderEngine[Class](/blog/619/) = {
// Declaring a method of an object
drawString: function (pString, pFontName, locX, locY, size, settings) {
// Declaring a variable and assinging to it:
var ctx = gRenderEngine.context;
// Accessing the member property 'font' of the 'ctx' object.
// Also shows the '+' operator overloaded to do string concatenation:
ctx.font = size + "pt " + pFontName;
// Single-line if block doesn't require surrounding braces:
if(settings.color) ctx.fillStyle = settings.color;
// Multi-line if blocks DO require surrounding braces:
if(settings.color) {
var maxWidth = settings.bounds.w;
// Calling the 'split' method of the 'pSpring' object.
var words = pString.split(" ");
var line = "";
var lineHeight = size + 4;
var = locY;
// for statements are syntactically similarly to C:
for(var n = 0; n words.length; n+) {
// Accessing the nth character in the 'words' string.
// All lists work similarly.
var testLine = line + words[n] + " ";
var metrics ctx.measureTest(testline):
var testWidth = metrics.width;
if(settings.bounds) {
ctx.fillText(line, locX, y);
line = word[n] + " ";
y += lineHeight;
// else statements can go on the same line as the
// closing brace of the preceding if statement, but
// they do not have to.
} else {
line = testLine;
}
}
ctx.fillText(line, locX, y);
} else {
if(settings.border[Color](/blog/423/)) {
// Single-line if block
}
JavaScript provides a way to define a set of variables and functions and to encapsulate them as an object, called a prototype.
Object-orientated set up can by emulated with JavaScript.
An inheritance tree can be created by using Class.extend() of the following form:
Weapon extends Class
MachineGun extends Weapon
ChainGun extends Weapon
Entity extends Class
Teleporter extends Entity
EnergyCanister extends Entity
Weapon = [Class](/blog/619/).extend({
init: function() {
}
});
MachineGun = Weapon.extend({
init: function() {
}
});
ChainGun = Weapon.extend({
init: function() {
}
});
Entity = [Class](/blog/619/).extend({
init: function() {
}
});
Teleporter = Entity.extend({
init: function() {
}
});
EnergyCanister = Entity.extend({
init: function() {
}
});
Web platform [http://docs.webplatform.org]
Mozilla Developer Network [http://developer.mozilla.org/en-US/]