This article mainly introduces the JavaScript constructor method to define objects. Friends who need it can refer to it
Javascript is a dynamic language. You can add attributes to the object at runtime and delete the object (delete). Attribute
Copy code The code is as follows:
<html> <head> <script type="text/javascript"> /* //01.定义对象第一种方式 var object =new Object(); alert(object.username); //01.1增加属性username object["username"]="liujianglong"; //object.username="liujl"; alert(object.username); //01.2删除属性username delete object.username;//username属性已经从object对象中删除 alert(object.username); */ //02.定义对象第二种方式--在javascript中定义对象的一种最常见的方式 var object={name:"zhangsan",age:10,sex:"fale"}; alert(object.name); alert(object.age); alert(object.sex); </script> </head> <body> </body> </html>
Attribute name: method name is also ok. Because the function itself is an object
javascript array sorting
Copy code The code is as follows:
<!DOCTYPE html> <html> <head> <script type="text/javascript"> var array=[1,3,25]; ///////////////////////////////// var compare=function(num1,num2){ var temp1=parseInt(num1); var temp2=parseInt(num2); if(temp1<temp2){ return -1; }else if(temp1==temp2){ return 0; }else{ return 1; } } //array.sort(compare);//01.函数名是对象引用 //////////////////////////////// //02.匿名函数方式////////////////// array.sort(function c(num1,num2){ var temp1=parseInt(num1); var temp2=parseInt(num2); if(temp1<temp2){ return -1; }else if(temp1==temp2){ return 0; }else{ return 1; } }); ///////////////////////////////// alert(array); </script> </head> <body> </body> </html>
Several ways to define objects in javascript (there is no concept of classes in javascript, only objects)
First One way: Expand its properties and methods based on existing objects
Copy code The code is as follows:
<script type="text/javascript"> //01.基于已有对象扩充其属性和方法 var object=new Object(); object.username="zhangsan"; object.sayName=function (name){ this.username=name; alert(this.username); } alert(object.username); object.sayName("lisi"); alert(object.username); </script>
This method has limitations, because javascript does not have the same features as java The concept of class, write a class, and then new can get an object with these attributes and methods.
If you want to own object2 at this time, you can only write another copy of the code above, which is not good.
Second method: Factory method
Similar to the static factory method in java.
Copy code The code is as follows:
<!DOCTYPE html> <html> <head> <script type="text/javascript"> //对象工厂方法 var createObject=function(){ var object=new Object(); object.username="zhangsan"; object.password="123"; object.get=function(){ alert(this.username+" , "+object.password); } return object; } var obj1=createObject(); var obj2=createObject(); obj1.get(); //修改对象2的密码 obj2["password"]="123456"; obj2.get(); </script> </head> <body> </body> </html>
There are disadvantages in creating objects in the above way (each object has a get method, thus wasting memory), after improvement Factory mode (all objects share a get method):
Copy code The code is as follows:
<!DOCTYPE html> <html> <head> <script type="text/javascript"> //所有对象共享的get方法 var get=function(){ alert(this.username+" , "+this.password); } //对象工厂方法 var createObject=function(username,password){ var object=new Object(); object.username=username; object.password=password; object.get=get;//注意:这里不写方法的括号 return object; } //通过工厂方法创建对象 var object1=createObject("zhangsan","123"); var object2=createObject("lisi","345"); //调用get方法 object1.get(); object2.get(); </script> </head> <body> </body> </html>
The third way: Define the object in constructor mode
Copy code The code is as follows:
<!DOCTYPE html> <html> <head> <script type="text/javascript"> var get=function(){ alert(this.username+" , "+this.password); } function Person(username,password){ //在执行第一行代码前,js引擎会为我们生成一个对象 this.username=username; this.password=password; this.get=get; //在此处,有一个隐藏的return语句,用于返回之前生成的对象[这点是和工厂模式不一样的地方] } var person=new Person("zhangsan","123"); person.get(); </script> </head> <body> </body> </html>
Fourth way: Prototype method to create objects
Prototype is an attribute in the object object, and all person objects are also You can have the prototype attribute.
You can add some attributes and methods to the prototype of the object.
Disadvantages of simply using the prototype method to create objects: ① Parameters cannot be passed, and its value can only be changed after the object is created.
Copy codeThe code is as follows:
<!DOCTYPE html> <html> <head> <script type="text/javascript"> function Person(){ } Person.prototype.username="zhangsan"; Person.prototype.password="123"; Person.prototype.getInfo=function(){ alert(this.username+" , "+this.password); } var person1=new Person(); var person2=new Person(); person1.username="lisi"; person1.getInfo(); person2.getInfo(); </script> </head> <body> </body> </html>
Copy code The code is as follows:
<!DOCTYPE html> <html> <head> <script type="text/javascript"> function Person(){ } Person.prototype.username=new Array(); Person.prototype.password="123"; Person.prototype.getInfo=function(){ alert(this.username+" , "+this.password); } var person1=new Person(); var person2=new Person(); person1.username.push("wanglaowu"); person1.username.push("wanglaowu2"); person2.password="456"; person1.getInfo (); person2.getInfo(); </script> </head> <body> </body> </html>
Simply using the prototype method to define an object cannot assign attributes to the constructor The initial value can only be changed after the object is generated. The fifth way: use the prototype constructor method to define the object----it is recommended to use
The properties between the objects do not interfere with each other
The same method is shared between each objectCopy code
The code is as follows:
<!DOCTYPE html> <html> <head> <script type="text/javascript"> //使用原型+构造函数方式来定义对象 function Person(){ //属性 定义在构造函数中 this.username=new Array(); this.password="123"; } //方法 定义在原型中 Person.prototype.getInfo=function(){ alert(this.username+" , "+this.password); } var p1=new Person(); var p2=new Person(); p1.username.push("zhangsan"); p2.username.push("lisi"); p1.getInfo(); p2.getInfo(); </script> </head> <body> </body> </html>
The sixth way: dynamic prototype method----it is recommended to use Pass the flag in the constructor Quantity allows all objects to share a method, and each object has its own properties.
Copy codeThe code is as follows:
<!DOCTYPE html> <html> <head> <script type="text/javascript"> var Person=function (username,password){ this.username=username; this.password=password; if(typeof Person.flag=="undefined"){ alert("invoked"); Person.prototype.getInfo=function(){ alert(this.username+" , "+this.password); } Person.flag=true; } } var p1=new Person("zhangsan","123"); var p2=new Person("lisi","456"); p1.getInfo(); p2.getInfo(); </script> </head> <body> </body> </html>
Related recommendations:
Javascript constructor, public, private privileges and static member definition methods_ javascript skillsThe above is the detailed content of javascript constructor method to define objects. For more information, please follow other related articles on the PHP Chinese website!