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.