There are several ways to define objects in 16.javascript (there is no concept of classes in JavaScript, only objects):
1) extend its properties and methods based on existing objects:
var New Object ();
= "Zhangsan";
function (name) {
this. Name = name;
Alert (this. Name);
}
Object.sayname ("Lisi");
2) Factory mode:
//factory-style object creationfunctionCreateObject () {varObject =NewObject (); Object.username= "Zhangsan"; Object.password= "123"; Object.get=function() {alert ( This. Username + "," + This. Password); }returnobject;}varObject1 =CreateObject ();varObject2 =CreateObject (); Object1.get ();//method of constructing with parametersfunctionCreateObject (Username,password) {varObject =NewObject (); Object.username=username; Object.password=password; Object.get=function() {alert ( This. Username + "," + This. Password); }returnobject;}varObject1 = CreateObject ("Zhangsan", "123"); Object1.get ();//let a function be shared by multiple objects, rather than having a function object for each objectfunctionget () {alert ( This. Username + "," + This. password);}functionCreateObject (Username,password) {varObject =NewObject (); Object.username=username; Object.password=password; Object.get=get; returnobject;}varObject1 = CreateObject ("Zhangsan", "123");varObject2 = CreateObject ("Lisi", "456"); Object1.get (); Object2.get ();
3) Constructor Method:
functionPerson () {//before executing the first line of code, the JS engine generates an object for USObject.username= "Zhangsan"; Object.password= "123"; Object.getinfo=function() {alert ( This. Username + "," + This. Password); }//here is a hidden return statement that returns the previously generated object}varperson =NewPerson ();p erson.getinfo ();//parameters can be passed when constructing an objectfunctionPerson (Username,password) {//before executing the first line of code, the JS engine generates an object for USObject.username=username; Object.password=password; Object.getinfo=function() {alert ( This. Username + "," + This. Password); }//here is a hidden return statement that returns the previously generated object}varperson =NewPerson ("Zhangsan", "123");p erson.getinfo ();
4) Prototype ("prototype") mode:
//create an object using the prototype (prototype) methodfunctionPerson () {}person.prototype.username= "Zhangsan"; Person.prototype.password= "123"; Person.prototype.getInfo=function() {alert ( This. Username + "," + This. password);}varPerson1 =NewPerson ();varPerson2 =NewPerson ();p erson1.username= "Lisi";p erson1.getinfo ();p erson2.getinfo ();//functionPerson () {}person.prototype.username=NewArray (); Person.prototype.password= "123"; Person.prototype.getInfo=function() {alert ( This. Username + "," + This. password);}varPerson1 =NewPerson ();varPerson2 =NewPerson ();p Erson1.username.push ("Zhangsan");p Erson1.username.push ("Lisi");p Erson1.password= "456";p erson1.getinfo ();p erson2.getinfo ();//defining objects using the Prototype + constructor methodfunctionPerson () { This. Username =NewArray (); This. password= "123";} Person.prototype.getInfo=function() {alert ( This. Username + "," + This. password);}varPerson1 =NewPerson ();varPerson2 =NewPerson ();p Erson1.username.push ("Zhangsan");p Erson2.username.push ("Lisi");p erson1.getinfo ();p erson2.getinfo ();
5) Dynamic prototyping: In the constructor, all objects share a method through the flag amount, and each object has its own properties.
functionPerson () { This. Username =NewArray (); This. password= "123"; if(typeofPerson.flag = = "undefined") {alert ("Invoked"); Person.prototype.getInfo=function() {alert ( This. Username + "," + This. Password); } Person.flag=true; }}varPerson1 =NewPerson ();varPerson2 =NewPerson ();p erson1.getinfo ();p erson2.getinfo ();
JS Note 2--defining objects