A preliminary study on JS object-oriented (top) JS Object-oriented 5 methods of writing

Source: Internet
Author: User

For a long time to see the online great God's JS code is particularly difficult, the object-oriented way of writing people to see the clouds in the fog. So the study of JS object-oriented, because it is a beginner, will find themselves on the Internet to collate the information, as a memory.

JS Object -oriented 5 methods of writing: (from http://www.iteye.com/topic/434462)

first define the Circle class, which has member variables R, a constant pi, and a member function area () that calculates the areas;            

The 1th notation  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 ());   


The 2nd way  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));  

The 3rd kind of notation  var Circle = new Object ();  Circle.pi = 3.14159;  Circle.area = function (r) {         return this. PI * R * r;  }    Alert (Circle.area (1.0));  

The 4th way  var circle={     "PI": 3.14159,   "area": function (r) {            return). PI * R * r;          }  ;  Alert (Circle.area (1.0));  

The 5th type is  var Circle = new Function ("this"). PI = 3.14159;this.area = function (r) {return r*r*this. PI;} ");    Alert ((New Circle ()). Area (1.0));  
   in the first method to use the Pretotype please see the next section JS in the Pretotype


A preliminary study on JS object-oriented (top) JS Object-oriented 5 methods of writing

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.