How to define JavaScript simple objects

Source: Internet
Author: User

Factory mode:
A novice developer might define an object like this:

var New  = "Hero"; obj.showname=function () {alert ( this

One problem here is that if we're going to use obj objects in multiple places, it's possible that similar code in the program will be written many times, resulting in a factory method

functionvarnew  Object (); Obj.name= "Hero"; obj.showname=  function () {alert (thisreturnvar obj1 =var 

And another way to construct a function method

functionthis. Name=thisfunction () {alert ( this

The code of the generated object is encapsulated to avoid duplicate new code, of course, it can be further improved, that is, Createobj pass some parameters, instead of assigning the default fixed value to obj:

functionvarnew  Object (); Obj.name=name; Obj.showname=  function () {alert (Thisreturnvar obj1 = Createobj ("Hero"   var

But one problem is that every time we call the Createobj function, we create a new function showname. This means that each object has its own version of ShowName, so it's better to avoid this problem.

function ShowName () {alert (This functionvarnew  Object () ; Obj.name=name; Obj.showname=return

This solves the problem of duplicate creation of the function, hahaha, done.

Prototype mode:
The main use is the prototype property of the object.

function obj () {} Obj.prototype.name= "Hero"; obj.prototype.showName=function() {alert (  This

Seems to be more perfect than the factory, but there is a problem, the function does not have a constructor, the property is specified by prototype, this is a real headache in practical applications, all the properties of the instance is not acceptable. In particular, there is a security risk, that is when the object has a reference , such as the addition of such a paragraph

Obj.prototype.nameArray = new Array ("Hero", "Dby");
And then
obj1 = new obj ();
Obj2 = new obj ();
Obj1.nameArray.push ("LxW");
This property will also be seen in Obj2 's NameArray, because the NameArray of two objects points to the same reference.
So this is not an ideal approach.
Need to improve
Combine constructors, define properties in constructors, define methods with prototypes
For example

this. Name =Thisnew Array ("Hero", "Dby"function() {alert (  this

All non-function properties are created in the constructor, the function attributes are created in the prototype mode, the value of the NameArray is changed in the Obj1, the NameArray value of the Obj2 object is not affected, and there is only one showname function, so there is no memory waste.

Basically perfect, the rest of the basic is a bit of other modifications. Interested can change their own play.
Here I add a single example to play a bit:

functionthis. Name =this. namearray=new Array ("Hero", "Dby"   if(typeof obj._initialized== ' undefined ') {obj.prototype.showName=function () {alert (this. name);} obj._initialized= "true"

In fact, is not a single case, but in the construction of the object, the first to determine whether a property is defined, if not defined, then use the prototype method to continue to define the object, if the property has been defined, then it is not a duplicate definition function. The prototype method is only created once and is evaluated once.

It's almost perfect again.
This is a personal understanding, and hope that everyone has help, not perfect the place please contact QQ, timely correction.
This is a complete example:

functionRectangle (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+ "<br/>"); document.write ( This. Area ()); } varobj1=NewRectangle ("Rectangle", "Red", 15,20); Obj1.show (); 

How to define JavaScript simple objects

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.