The way you define objects in two simple javascript:
In JavaScript, you can dynamically add properties to an object, or you can dynamically delete an object's properties.
1 var object=New object (); 2 // alert (object.username); 3 object["username"]= "Zhangsan"; 4 alert (object.username); 5 Delete // username has been removed from object objects 6 alert (object.username);
The most common way to define objects in javascript:
1 Var object ={username: "Zhangsan", password:123};
There are several ways to define objects in JavaScript (there is no concept of classes in JavaScript, only objects ):
1) extend its properties and methods based on existing objects:
1 varObject =NewObject ();2 3Object.name = "Zhangsan";4Object.sayname =function(name)5 {6 This. Name =name;7Alert This. Name);8 }9 TenObject.sayname ("Lisi");
2) Factory mode:
In order for a function object to be shared by more than one object, rather than having a function object for each object, the member function of the object should be defined as a foreign invocation of CreateObject (username, password) instead of placing the function within the factory method.
1 //To create an object using the factory method2 functionget ()3 {4Alert This. Username + "," + This. password);5 }6 7 functionCreateObject (username, password)8 {9 varObject =NewObject ();Ten OneObject.username =username; AObject.password =password; - -Object.get =get; the - returnobject; - } - + varObject = CreateObject ("Zhangsan", "123"); - varObject2 = CreateObject ("Lisi", "456"); + A object.get (); atObject2.get ();
3) Constructor Method:
1 functionPerson ()2 {3 //before executing the first line of code, the JS engine generates an object for US4 This. Username = "Zhangsan";5 This. Password = "123";6 7 This. GetInfo =function()8 {9Alert This. Username + "," + This. password);Ten } One A //here is a hidden return statement that returns the previously generated object - } - the varperson =NewPerson (); -Person.getinfo ();
Parameters can be passed when constructing an object
1 functionPerson (username, password)2 {3 This. Username =username;4 This. Password =password;5 6 This. GetInfo =function()7 {8Alert This. Username + "," + This. password);9 }Ten } One A varperson =NewPerson ("Zhangsan", "123"); -Person.getinfo ();
4) Prototype ("prototype") mode:
Simply by using a prototype to create an object, all of the generated objects share the properties in the prototype, so that an object changes the property and also reacts to other objects. At the same time, an object cannot assign an initial value to a property in a constructor, but only after the object has been generated.
1 functionPerson ()2 {3 4 }5 6Person.prototype.username =NewArray ();7Person.prototype.password = "123";8 9Person.prototype.getInfo =function()Ten { OneAlert This. Username + "," + This. password); A } - - varperson =NewPerson (); the varPerson2 =NewPerson (); - -Person.username.push ("Zhangsan"); -Person.username.push ("Lisi"); +Person.password = "456"; - + person.getinfo (); APerson2.getinfo ();
Defining objects using the Prototype + constructor method solves these problems:
1 //defining objects using the Prototype + constructor method2 3 functionPerson ()4 {5 This. Username =NewArray ();6 This. Password = "123";7 }8 9Person.prototype.getInfo =function()Ten { OneAlert This. Username + "," + This. password); A } - - varp =NewPerson (); the varP2 =NewPerson (); - -P.username.push ("Zhangsan"); -P2.username.push ("Lisi"); + - p.getinfo (); +P2.getinfo ();
5) Dynamic prototyping: In the constructor, all objects share a method through the flag amount, and each object has its own properties.
1 functionPerson ()2 {3 This. Username = "Zhangsan";4 This. Password = "123";5 6 if(typeofPerson.flag = = "undefined")7 {8Alert ("invoked");9 TenPerson.prototype.getInfo =function() One { AAlert This. Username + "," + This. password); - } - thePerson.flag =true; - } - } - + varp =NewPerson (); - varP2 =NewPerson ();
The object of JavaScript