Java Reference
In-Depth Information
Object Literals
An object in JavaScript is a self-contained set of related values and functions. They act as
a collection of named properties that map to any JavaScript value such as strings, numbers,
Booleans, arrays, and even functions. If a property's value is a function, it is known as a
method .
One way to think about an object is that it's like a dictionary where you look up a property
name and see a value. Another way of is that it is like a database of values; some databases
actually use JavaScript objects to store information. This is similar to a hash or associative
array in other programming languages; however, JavaScript objects are much more flexible
as they can be employed to encapsulate code that can be reused throughout a program. They
can also inherit properties from other objects (which we'll cover in Chapter 11 ) .
An object literal is an object that is created directly in the language by wrapping all its
properties and methods in curly braces {} . Object literals are a distinguishing feature of the
JavaScript language, as they allow objects to be created quickly without the need for a class
template.
A Super Example
Here is an example of an object literal that describes the Man of Steel:
var superman = {
name: "Superman",
"real name": "Clark Kent",
height: 75,
weight: 235,
hero: true,
villain: false,
allies: ["Batman","Supergirl","Superboy"],
fly: function(){
return "Up, up and away!";
}
}
 
Search WWH ::




Custom Search