JavaScript Object
An object is a collection of properties. A property is a associate between a name and a value.
for...in
can be used to iterate over all the enumerable properties of an object.
var myFan = {
make: 'Honeywell',
blades: 4,
rotation: 'on'
}
for (var i in myFan) {
if (myFan.hasOwnProperty(i)) {
console.log(i + " :: " + myFan[i]);
}
}
// outputs
// make :: Honeywell
// blades :: 4
// rotation :: on
Ways to create objects
Object initializers
var rectangle = {width: 10, height: 100};
Constructor function
function TV(make, model) {
this.make = make;
this.model = model;
}
var myTv = new TV('LG', 'ThinQ');
Object.create
Allows to choose the prototype object for the object you want to create, without having to define a constructor function.
var bike = {
wheels : 2,
gear: 6
}
var myBike = Object.create(bike);
References:
Categories :
JavaScript