JavaScript Objects
An object is a is a collection of properties, otherwise called an associative array.
Objects can be created in one of two ways, in literal notation, or by using a constructor.
The preferred method, literal notation, puts all the properties witin a set of brackets when the object is defined:
var train = { name: "Silver Meteor", passengers: 76 };The second approach consists of creating a constructor, which explicitly creates an object. You can then add properties through dot notation.
var train = new Object(); train.name = "Silver Meteor"; train.passengers = 76;The object constructor uses one of two formats. Both are functionally the same, but the first, called object notation, is generally preferred.
var myObject = new Object(); myObject.name = "Joab"; myObject.age = 25; myObject.gender = "M";or...
var myObject3 = {}; myObject["name"] = "Joab"; myObject["age"] = 25; myObject["gender"] = "M";
var myArray = [2, 3, 4]; var myObject = { name: 'Eduardo', type: 'Most excellent', interests: myArray, };It can even hold other objects:
var friends = { steve: {name: "Steve Jobs"}, bill: {name: "Bill Gates"}, };
In addition to data, you can also add methods to an object. A method is a function that can be called through the object. A function performs some operation, such as adding two numbers together. You can pass a function values, i.e. "("Silver Meteor", 76)" or not "()"
Some architectual underpinnings! In JavaScript, almost everything is an object.
The Window is the global object for a browser. All variables are defined as properties of a window, i.e. "var answer = 42" = "window.answer = 42" (window can also be called "self"). "document" is the document object of the current window.
All objects in JavaScript are arrays. A variable, for instance, is an object and comes with a number of properties, which can be accessed, i.e.
var SampleData = new String("This Is The Sample Data");
(...By using the new keyword, you establish the string as a primitive, rather than an object)
alert(SampleData.length)
(...Accesses the length property of the string)
alert(SampleData.toUpperCase());
(...Converts string to upper case)
Other properties can be used to extract a substring, to concatenate multiple strings together, to slice off a piece of the string, to tokenize a string, to can create an HTML tag, among others.