Pages

Saturday, October 1, 2016

Objects With Eyes (JavaScript)

In JavaScript, an object with a property called eyeColor can be created like this:
{ eyeColor: "blue" }
The same object can be created in two steps, using a variable called 'x' to hold the value of the object, like this:
var x = {};
x.eyeColor = "blue";
The same object can also be created using a constructor function like this:
function ObjectWithEyes(EyeColor) { this.eyeColor = EyeColor; }
new ObjectWithEyes("blue");
The following image of several JavaScript statements being executed one after another demonstrates all three techniques. Click on the image to view a full-size version.