Object
Two ways to initialize an object:
- var object1 = { };
- var object2 = new Object( );
We could put property in object, it looks like a dictionary in other language.
- var object1 = { property1 = xxx, property2 = xxx};
- object2[“property1”] = xxx;
- object2.property1 = xxx;
We could put method in object, we could declare it in the object, or declare it outside the object, and using this key word, to assign the method to the object as if it is a variable.
- var fun1 = function (param) { this.property1 = xxx;};
- object2.fun1 = fun1;
- object2.fun2 = function (param) { this.property1 = xxx;};
We could also write constructor instead of using Object( ).
function Person(name, age) {
this.name = name;
this.age = age;
this.fun1 = function( param ) {
xxx
};
}
Use typeof object to see the type of a variable.
Use hasOwnProperty to see an object has a property or not.
Class.prototype for all class.
Inherits
- Penguin.prototype = new Animal( );
All properties are by default public. Use var instead of this keyword would give private property.
没有评论:
发表评论