The _javascript skill of JS object-oriented drafting

Source: Internet
Author: User

This article summed up the JS object-oriented several common writing, share to everyone for your reference, the specific contents are as follows
1. Factory mode

var Circle = function () {
 var obj = new Object ();
 Obj. PI = 3.14159;
 
 Obj.area = function (r) {return this
  . PI * R * r;
 }
 return obj;
}

var c = new Circle ();
Alert (C.area (1.0));

2. More formal wording

function Circle (r) {
  THIS.R = R;
}
Circle.pi = 3.14159;
Circle.prototype.area = function () {return
 Circle.pi * THIS.R * THIS.R;
}

var c = new Circle (1.0); 
Alert (C.area ()); 

3.json wording

var circle={
 "PI": 3.14159,
 ' area ': function (r) {return this
   . PI * R * r;
  }
};
Alert (Circle.area (1.0));

4. It's a bit of a change, but the essence is the first

var circle=function (r) {
  this.r=r
}
Circle.pi = 3.14159; 
circle.prototype={
 area:function () {return
  this.r*this.r*circle.pi;
 }
}
var obj=new Circle (1.0);
Alert (Obj.area ()

Circle.pi = 3.14159; Can be put into the attribute to write this. pi=3.14159;

Used as the first and third, the third way to extend the small example

var show={
  btn:$ ('. Div1 '),
  init:function () {
   var that=this;
   alert (this);
   This.btn.click (function () {
     that.change ();
     alert (this);
    })
   
  ,
  change:function () {
   this.btn.css ({' Background ': ' Green '});

  }
 Show.init ();

Note that this is the point of the issue, the following is a little introduction to this , I hope to help.
A dynamic prototyping method is used to create a custom object in JS, this is also very smooth.
This method is used for the creation and use of variables within an object using the "this." The beginning.
For example: Object Contactmodel, there are three attributes , Crtnewfriendlistlen,crtnewfriendlist,crtfindeduserid
And four methods requestcontactlist (), Requestnewfriendlist (), Requestfinduser (), Requestaddcontact ()
To access your own properties inside this variable, take "this."

var Contactmodel;
...
Contactmodel = new Contactmodel ();
 function Contactmodel () {//this.contactlist;
 This.crtnewfriendlistlen;
 This.crtnewfriendlist;
 
 This.crtfindeduserid = "-1";
   
  if (typeof contactmodel._initialized = = "undefined") {ContactModel.prototype.requestContactList = function () { } ContactModel.prototype.requestNewfriendList = function () {} ContactModel.prototype.requestFindUser = f Unction (UserID) {$.getjson (Mainurl + "User/getuserinfo", {"UID": userid},function (resultobj) {//THIS.CR
    Tfindeduserid = UserID Contactmodel.crtfindeduserid = UserID;
   UiManager.contectAddPage.receiveFindUserResult (Resultobj);
  });
    
  } ContactModel.prototype.requestAddContact = function (remark) {alert (This.crtfindeduserid);
 } contactmodel._initialized = true;
}; }

But then the problem arises, and in Requestfinduser (), if you use This.crtfindeduserid to store the value from the server, then after this object is called the Requestaddcontact () method, is not able to get the value of Crtfindeduserid, the alert will still show the initial value of 1, the problem is in the $.getjson () callback method, at this point is not contactmodel instance, but this method body, So the solution here is to get the Contectmodel instance within the callback method and assign the property Crtfindeduserid to the instance.
within an object's listener callback method for the view component, this does not point to the object itself, but also to the callback method body, and to access the properties of the object itself, it is necessary to get an instance of the object to access it instead of this.
Below is a paragraph JS object-oriented standard notation :

 
 

I hope this article will help you learn about JavaScript programming.

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.