the objects in javascript are not similar to that of 'C++'
the objects can store all kinds of variables , including functions.
objects are like arrays, but in arrays the keys are numbers and values can be in anything
but in objects , keys can be anything as well.
the syntax for objects is
var object = {
key : value,
key : value,
key :value
};
ex:
var obj = {
name : "felix",
age : 22
};
***actually , it is curious to see that there is no concept of 'class' in javascript. All the object are prototype-based inheritance schemes.
new objects are created by copying other objects(instead of being instantiated from a class template)
methods live directly in objects instead of classes
inheritance is done through delegation
Javascript has a nice concept tweak of constructors, you can copy existing objects with constructors as well create objects out of thin air
*** two important articles about inheritance are here
http://javascript.crockford.com/prototypal.html
http://javascript.crockford.com/inheritance.html
we can actually create objects here in two ways
the previous above method is first method with the use of {}
the second method is by the use of the constructors.
ex:
var myobject = new Object(); //this tells computer to make a new object type
myobject.name = "felix"; //we can use this method or method below which uses key
myobject["age"] = 22;
No comments:
Post a Comment