recently in reading the JavaScript Advanced Program design, is a bit forgotten to read the content, it is intended to do some reading in the form of blog notes. Today is about Four ways to define objects in JavaScript, in addition to these four methods, there are factory methods to define objects, but given their simplicity and informality, this is not an introduction. compared to object-oriented languages such as Java , JavaScript is more like a functional language, and there is no concept of class, lid the concept of object definition, The object created is called an instance of the object.
1) The constructor defines the object as a function. in this way, properties and methods are defined within the constructor function. Here's a simple example:
function Animal (name) {
THIS.name = name;
This.introduceself = function () {
Window.alert ("I am a" + this.name+ "!");
};
}
The instantiation of the object is as follows:
var dog = new Animal ("Dog");
Dog. Introduceself ();
Of course , the object's methods can also be defined outside the constructor, as follows:
function Introduceself () {
Window.alert ("I am a" + this.name+ "!");
}
function Animal (name) {
THIS.name = name;
This.introduceself = Introduceself ();
}
This way of defining all the properties and methods within the constructor makes the constructor equivalent to the concept of classes in Java . Instead of putting the concrete definition of the method in the constructor, the definition and method of the object are not tightly defined, and if such a method and object are defined more in a file, it will look messy.
2) prototype mode. This approach takes advantage of the prototype property of an object and can be seen as the prototype on which the new object is created (about prototype more information, you can Baidu, this information is still a lot of. This way the constructor is made into an empty constructor, and then all the properties and methods are given directly to the prototype Property, or the previous example, rewritten as follows:
function Animal () {
}
Animal.prototype.name = "Animal";
Animal.prototype.introduceSelf = function () {
Window.alert ("I am a" + this.name+ "!");
};
An instance of the object is as follows:
var dog = new Animal ();
Dog.name = "Dog";
Dog.introductself ();
The disadvantage of this approach is that it is not possible to assign a property to an attribute in the constructor. Also, for a property of a reference type, such as an Array instance, multiple objects that are instantiated share a reference. Because of so many problems, the way this object is defined is not desirable.
3) constructor, prototype blending mode. This approach is the most popular way to define objects. It combines constructors and prototypes. In this way, all non-function properties of an object are defined using constructors, and the function properties (methods) of the object are defined in a prototype manner. Rewrite the previous example:
function Animal (name) {
THIS.name = name;
}
Animal.prototype.introduceSelf = function () {
Window.alert ("I am a" + this.name+ "!");
};
4) dynamic prototyping mode. compared with constructors and prototypes, the dynamic prototyping method is only different in the location defined by the function attribute. Rewrite the previous example:
functionAnimal (name) {
This. Name=name;
if (typeofanimal._initialized==' undefined ') {
Animal.prototype.introduceSelf=function(){
Window.alert ("I am a"+ This . Name+"!");
};
Animal._initialized=true;
}
}
Where _initialized is the global private property of animal (there is no concept of private property in JavaScript, all properties are common, but to indicate the private nature of some properties, it is customary to precede the property name with "_". ), when animal is instantiated for the first time, the if condition is true, which defines the function properties in the animal. Because "animal._initialized = true;" , the definition of a function is called once, regardless of how many objects are instantiated.
Four ways to define objects in JavaScript 2012-5-10 15:19 read (0)