A brief discussion on several common JS class definition methods _javascript skills

Source: Internet
Author: User

Method 1 Object Direct Quantity

var obj1 = {
 v1: "",
 get_v1:function () {return
  this.v1;
 },
 set_v1:function (v) {
  this.v1 = v;
 }
};

Method 2 defines a function object

var Obj = function () {
 var v1 = "";
 THIS.GET_V1 = function () {return
  this.v1;
 };
 THIS.SET_V1 = function (v) {
  this.v1 = v;
 }
};

Method 3 Prototype Inheritance

var Obj3 = new Function ();
Obj3.prototype = {
 v1: "",
 get_v1:function () {return
  this.v1;
 },
 set_v1:function (v) {
   
    this.v1 = v;
 }
;
   

Method 4 Factory mode

function Loadobj () {
 var tmp = new Object ();
 TMP.V1 = "";
 TMP.GET_V1 = function () {return
  tmp.v1;
 };
 TMP.SET_V1 = function (v) {
  tmp.v1 = v;
 };
 return tmp;
}

OBJ1.SET_V1 (' Hello1 ');
Alert (OBJ1.GET_V1 ());

var obj2 = new Obj ();
OBJ2.SET_V1 (' Hello2 ');
Alert (OBJ2.GET_V1 ());

var obj3 = new Obj ();
OBJ3.SET_V1 (' Hello3 ');
Alert (OBJ3.GET_V1 ());

var obj4 = Loadobj ();
OBJ4.SET_V1 (' Hello4 ');
Alert (OBJ4.GET_V1 ());

alert (obj1);
alert (OBJ2);
alert (OBJ3);
alert (OBJ4);

The above article discusses several commonly used JS class definition method is the small series to share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

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.