Home  >  Article  >  Web Front-end  >  JavaScript object definition method is simple and easy to learn_js object-oriented

JavaScript object definition method is simple and easy to learn_js object-oriented

WBOY
WBOYOriginal
2016-05-16 18:55:01873browse

Factory pattern:
Junior developers may define objects like this:
var obj = new Object();
obj.name = "hero";
obj.showName=function (){alert( this.name);}
A problem here is that if we want to use the obj object in multiple places, we may have to write similar code many times in the program, so the factory method
function createObj()
{
var obj = new Object();
obj.name="hero";
obj.showName=function (){alert(this.name);}
return obj;
}
var obj1 = createObj();
var obj2 = createObj();
and another method constructor method
function obj (name)
{
this.name=name;
this.showName= function (){alert(this.name);}
}
Encapsulate the code that generates the object to avoid duplicating new code. Of course, you can also A further improvement is to pass some parameters to createObj instead of assigning a default fixed value to obj:
function createObj(name)
{
var obj = new Object();
obj.name=name;
obj.showName=function (){alert(this.name);}
return obj;
}
var obj1 = createObj("hero");
var o'b' j2=createObj("dby");
But there is a problem, that is, every time we call the createObj function, a new function showName will be created. This means that each object has its own version of showName, so we need to improve to avoid this problem. .
function showName()
{
alert(this.name)
}
function createObj(name)
{
var obj = new Object();
obj.name=name;
obj.showName=showName;
return obj;
}
This solves the problem of repeatedly creating functions, hahaha, you’re done.
Prototype method:
Mainly use the prototype attribute of the object.
function obj()
{}
obj.prototype.name="hero";
obj.prototype.showName=function()
{
alert(this.name);
}
It seems to be more perfect than the factory just now, but there is a problem. This function has no constructor and the attributes are specified through prototype. This is In practical applications, it is very troublesome. It is really unacceptable that the attributes of all instances are the same. In particular, there is a security risk, that is, when there are references in the object, for example, adding such a paragraph
obj.prototype. nameArray = new Array("hero","dby");
Then
obj1 = new obj();
obj2 = new obj();
obj1.nameArray.push("lxw" );
You will also see this attribute in the nameArray of obj2, because the nameArray of the two objects points to the same reference.
So this method is not ideal.
Needs improvement
Combine the constructor, define properties in the constructor, and use prototype definition methods
For example,
fuction obj(name)
{
this.name = name
this.nameArray = new Array(" hero","dby");
}
obj.prototype.showName = function(){alert(this.name)}
All non-function properties are created in the constructor, and function properties are created using Created in prototype mode, changing the nameArray value in obj1 will not affect the nameArray value of the obj2 object, and there is only one showName function, so there is no memory waste.
Basically perfect, the rest are basically other things Modified. If you are interested, you can change it and play with it yourself.
The author added a singleton here to play with:
function obj (name)
{
this.name = name;
this.nameArray=new Array("hero","dby");
if(typeof obj._initialized=='undefined')
{
obj.prototype.showName=function(){ alert(this.name);}
obj._initialized="true";
}
}
In fact, it is not a singleton. It is just that when constructing an object, it first determines whether an attribute is defined. If it is not defined, then use the prototype method to continue defining the object. If the attributes have been defined, then the function will not be defined again. The prototype method is only created once and assigned once.
It’s almost perfect again, and you’re done. .
This is my personal understanding, and I hope it will be helpful to everyone. If there are any imperfections, please contact QQ and correct them in time.
This is a complete example:
function Rectangle(name,color,width,heigth){
this.name=name;
this.color=color;
this.width= width;
this.heigth=heigth;
}
Rectangle.prototype.area=function(){
return this.width*this.heigth
}
Rectangle.prototype. show=function(){
document.write(this.name " " this.color " " this.width " " this.heigth "
");
document.write(this.area() );
}
var obj1= new Rectangle("Rectangle","red",15,20);
obj1.show();

Statement:
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