Home > Web Front-end > JS Tutorial > js object-oriented summary of various methods of creating objects_js object-oriented

js object-oriented summary of various methods of creating objects_js object-oriented

WBOY
Release: 2016-05-16 17:53:43
Original
1219 people have browsed it

Start creating objects:

 1. Object literal.

Copy code The code is as follows:

var clock={
hour:12,
minute:10,
second:10,
showTime:function(){
alert(this.hour ":" this.minute ":" this.second);
}
}
clock.showTime();//Call

2. Create Object instance
Copy code The code is as follows:

var clock = new Object();
clock.hour=12;
clock.minute=10;
clock.showHour=function (){alert(clock.hour);};

clock.showHour();//Call

This shows that attributes can be dynamically added and modified



Object creation mode:

1. Factory mode: It is a function, then puts in parameters, returns the object, and the pipeline works
Copy code The code is as follows:

function createClock(hour,minute,second){
var clock = new Object();
clock.hour=hour;
clock.minute=minute;
clock.second=second;
clock.showHour=function(){
alert(this.hour ":" this.minute " :" this.second);
};
return clock;
};
var newClock = createClock(12,12,12);//Instantiate
newClock.showHour() ;//Call

Advantages: Finally, advantages are an abstract concept. But the type of object cannot be recognized!

2. Constructor pattern
Copy code The code is as follows:

function clock(hour,minute,second){
this.hour = hour;
this.minute = minute;
this.second = second;
this.showTime = function(){
alert(this.hour ":" this.minute ":" this.second);
}
}
var newClock =new clock(12,12,12);
alert(newClock. hour);

Note: The new keyword is required. If it is not added, clock will not be called as a constructor, but just an ordinary function. At the same time, attributes will be accidentally added to his outer scope, window, because this inside the constructor has been mapped to the outer scope at this time. So to be on the safe side, you can create
Copy the code The code is as follows:

function clock(hour ,minute,second){
if(this instanceof clock){
this.hour = hour;
this.minute = minute;
this.second = second;
this.showTime = function(){
alert(this.hour ":" this.minute ":" this.second);
}
}
else{
throw new Error("please add ' new' to make a instance");
}
}

Disadvantages of the constructor: Because the this pointer changes when the object instance is used, it points to a new instance. At this time, the methods of the new instance must also be re-created. If there are n instances, the same method must be re-created n times. So let’s uncover the prototype mode

3. Prototype mode
Copy the code The code is as follows:

function clock(hour,minute,second){
}
clock.prototype.hour=12;
clock.prototype.minute=12;
clock.prototype. second=12;
clock.prototype.showTime=function(){
alert(this.hour ":" this.minute ":" this.second);
}
var newClock = new clock();
newClock.showTime();

It is important to have a deep understanding of the prototype model.

First of all, each function has a prototype (prototype) attribute, and this pointer points to the clock.prototype object. This prototype object has a property constructor by default, pointing to clock. This property is readable and writable. When we instantiate an object, in addition to the properties and methods defined by the constructor (note, only in the constructor), the instance newClock also has a pointer to the prototype of the constructor, which ECMAScript calls [[ prototype]], when instantiating an object, the methods of the prototype object are not in a specific instance, because the prototype has not been instantiated. (There is a lot of nonsense, I didn’t mislead you. Don’t be confused)

So for the object defined in this mode, when calling the method: call newClock.showTime(); first check whether there is any in the instance, and there is a call Therefore, there is no prototype traced, there is no error, and the call fails.

Of course you can write it like this:
Copy the code The code is as follows:

function clock(hour,minute,second){
}
clock.prototype={
constructor:clock, //This property must be set manually, otherwise the connection with the constructor will be broken. There is no point in instance sharing prototype anymore.
hour:12,
minute:12,
second:12,
showTime:function(){
alert(this.hour ":" this.minute ":" this.second )
}
}
var newClock = new clock();
newClock.showTime();

Note: The connection between the instance and the constructor prototype is also through a pointer To contact, so you can dynamically add methods to modify the prototype.

The problem with this pure prototype model is also obvious. All attributes and methods are shared, and objects cannot be made concrete. Often we think of each object as having its own properties. Therefore, combining the first two, a new model is generated

4. Construction-prototype combination mode.
Copy code The code is as follows:

function clock(hour,minute,second){
this.hour = hour;
this.minute = minute;
this.second = second;
}

clock.prototype.showTime=function(){alert(this.hour ":" this.minute ":" this.second);}
var newClock = new clock(12,12,12);
newClock.showTime();

here We put the properties in the constructor to make the object more specific.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template