Some simple ways to define a JavaScript object
1. Constructor mode, all properties and methods of the object are defined in the constructor method.
Advantages : Dynamic transfer of parameters
cons : Every object created will create the same method function object, consuming a lot of memory
functionUser1 (name, password) { This. Name =name; This. Password =password; This. Login =function(name, password) {if( This. Name = = Name && This. Password = =password) { return true; } Else { return false; } };}
2. Prototype mode, the object properties and method definitions are written in the object's prototype inside
Pros : Every time you create an object, you use the same method in the same prototype, without consuming duplicate memory
Cons : Unable to dynamically pass construction parameters
functionUser2 () {}user2.prototype.name= "Prototype Name"; User2.prototype.password= "Prototype Password"; User2.prototype.login=function(name, password) {if( This. Name = = Name && This. Password = =password) { return true; } Else { return false; }};
Another way of prototyping:
format:
var object name = {};
object name. prototype. Variable 1 = value of variable 1;
object name. prototype. variable 2 = value of variable 2;
...;
object name. prototype. function 1= functions () {
function Body
};
object name. prototype. function 2= functions () {
function Body
};
...;
Description:
(1) The initial object body may not define anything;
(2) Add "object name. Prototype" before the variable to be defined. The format;
(3) The content and value of the object are separated by an equal sign and appear in pairs;
(4) The included variables or functions are separated by semicolons, or you can omit semicolons.
(5) The function needs to be written inside the curly braces of functions () {}.
Example:
var data = {}; = "Vicky"; Data.prototype. Age =20; Data.prototype. Eat = function () { alert (' I wanna Eat Meat '); }; Data.prototype. sleep= function () { alert (' I wanna Sleep ');};
3. Construction and prototype blending method, write the attribute definition in the construction mode, the method is written in prototype
Advantages : Combining the advantages of the construct and prototype modes, you can dynamically transfer the construction parameters, and the method function object creates only one
cons : Functions are written outside of the object and are not quite consistent with object-oriented thinking
functionUser3 (name, password) { This. Name =name; This. Password =password;} User3.prototype.login=function(name, password) {if( This. Name = = Name && This. Password = =password) { return true; } Else { return false; }};
4. Dynamic Prototyping method: Add a Judgment attribute to determine whether the object has been created, if it was created, then the method is not built
Advantage : Write the prototype function inside the object definition
cons : inheritance is not supported
functionUser4 (name, password) { This. Name =name; This. Password =password; if(typeofuser4.__initialized = = "undefined") {User4.prototype.login=function(name, password) {if( This. Name = = Name && This. Password = =password) { return true; } Else { return false; } }; User4.__initialized=true; }}
5. JSON mode/Object Direct volume
format:
var Object name = {
Variable 1: The value of variable 1,
Variable 1: The value of variable 1,
. ....,
function 1:function () {
function Body
},
function 2:function () {
function Body
}//note: The last comma to be removed in order to be compatible with IE.
};
Description:
(1) A variable or function is directly filled in curly braces;
(2) The contents and values of the object are separated by colons and appear in pairs;
(3) The variables or functions included are separated by commas;
(4) The function needs to be written inside the curly braces of functions () {}.
Example:
var object name = { name: "Vicky", +, function () { alert (' I wanna Eat meat '); c9/>}, function () { alert (' I wanna Sleep ');} };
Note: A similar approach is called anonymous class
Anonymous class Examples:
{
Index: '//',
Reg:new RegExp (' ^//.*$ '),
CSS: "comment"
}
The above method creates a class, but does not assign a variable to it.
6. Create mode
This approach leverages the prototype JavaScript component library.
format:
var object name = Class.create ();
object.extend (object name. prototype, {
Variable 1: The value of variable 1,
Variable 1: The value of variable 1,
. ....,
function 1:function () {
function Body
},
function 2:function () {
function Body
},
...
});
Description:
(1) The object is created using the Class.create () function in the prototype library;
(2) The content of the object is extended using the Object.extend () function in the prototype library;
(3) The extended object must take prototype when passing in the Object.extend function .
(4) The extension content is enclosed in curly braces, which is exactly the same as the JSON-defined format.
Example:
var data = class.create (); Object.extend (Dta.prototype, { "Vicky", function () { alert (' I wanna Eat Meat '); }, function () { Alert (' I wanna Sleep ');} });
In fact, the definition of JS objects There are other ways, you can also use the above several combinations of definitions, which shows that JS as a dynamic language of freedom.
The normal way to create a JS object is as follows:
var D1 = new Data ();
There are two ways to refer to a JS object variable:
(1) Point number method reference, such as, Data.name.
(2) array mode reference, e.g., data[' name ']
JavaScript defines object notation