Object Literal provide a very convenient notation for creating new object values. An object literal is a pair of curly braces surrounding zero or more name/value pairs.
var empty_obj={};
var stooage={"firstname":"Gen","lastname":"Linux"}
Objects can nest.
var flight={
air:"BA",number:"335",departure:{Gate:"45"}
}
Undefined value is produced if an attempt is made to retrieve a non-existent member.
stooage.status // undefined
flight["area] // undefined
Attempting to retrieve values from undefined will throw a TypeError exception.
flight.equipment // undefined
flight.equipment.model // throw "TypeError"
This can be guarded against with the && operator:
flight.equipment && flight.equipment.model // undefined
Reference
Objects are passed around by reference. They are never copied:
var x=stooge;
x.nickname = 'Curly';
var nick = stooge.nickname;
// nick is 'Curly' because x and stooge
// are references to the same object
Function Literal
Function objects are created with function literals:
// Create a variable called add and store a function
var add = function (a, b) {
return a + b;
};
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment