JavaScript constructor Way to define objects _ basics

Source: Internet
Author: User
Tags javascript array

JavaScript is a dynamic language, you can add properties to an object at run time, or you can delete (delete) attributes to an object

Copy Code code as follows:

<script type= "Text/javascript" >
/*
01. Define the first way of the object
var object =new object ();
alert (object.username);
01.1 Adding attributes Username
object["username"]= "Liujianglong";
Object.username= "LIUJL";
alert (object.username);
01.2 Delete attribute username
Delete Object.username;//username property has been removed from object
alert (object.username);
*/
02. The second way to define objects--one of the most common ways to define objects in JavaScript
var object={name: "Zhangsan", Age:10,sex: "Fale"};
alert (object.name);
alert (object.age);
alert (object.sex);
</script>
<body>
</body>

Property Name: Method name is also possible. Because the function itself is an object

JavaScript array sorting

Copy Code code as follows:

<! DOCTYPE html>
<script type= "Text/javascript" >
var array=[1,3,25];
/////////////////////////////////
var compare=function (num1,num2) {
var temp1=parseint (NUM1);
var temp2=parseint (num2);
if (TEMP1&LT;TEMP2) {
return-1;
}else if (TEMP1==TEMP2) {
return 0;
}else{
return 1;
}
}
Array.Sort (compare);//01. Function name is an object reference
////////////////////////////////

02. anonymous function Mode//////////////////
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>
<body>
</body>

Several ways to define objects in JavaScript (no concept of class in JavaScript, only objects)

The first way: expand its properties and methods based on existing objects

Copy Code code as follows:

<script type= "Text/javascript" >
01. Expand its properties and methods based on existing objects
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 approach has limitations because JavaScript does not have a class concept like Java, writing a class, and then new can get an object with these properties and methods.

At this point if you want to have object2 can only write the code above, this is not very good.

The second way: Factory way

Similar to the static factory method in Java.

Copy Code code as follows:

<! DOCTYPE html>
<script type= "Text/javascript" >
Object Factory method
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 ();
Modify the password for object 2
obj2["Password"]= "123456";
Obj2.get ();
</script>
<body>
</body>

There are drawbacks to creating objects in this way (each object has a Get method, which wastes memory), and the improved factory approach (all objects sharing a Get method):

Copy Code code as follows:

<! DOCTYPE html>
<script type= "Text/javascript" >
All object-Shared Get methods
var get=function () {
Alert (this.username+ "," +this.password);
}
Object Factory method
var createobject=function (Username,password) {
Var object=new object ();
Object.username=username;
Object.password=password;
object.get=get;//Note: The parentheses of the method are not written here
return object;
}
Creating objects from the factory method
var object1=createobject ("Zhangsan", "123");
var object2=createobject ("Lisi", "345");
Call Get method
Object1.get ();
Object2.get ();
</script>
<body>
</body>

The third way: To define an object in a constructor way

Copy Code code as follows:

<! DOCTYPE html>
<script type= "Text/javascript" >
var get=function () {
Alert (this.username+ "," +this.password);
}
function Person (Username,password) {
The JS engine generates an object for us before executing the first line of code
This.username=username;
This.password=password;
This.get=get;
Here, there is a hidden return statement that returns the previously generated object [this is not the same place as the factory pattern]
}
var person=new person ("Zhangsan", "123");
Person.get ();
</script>
<body>
</body>

Fourth Way: Prototype (Prototype) way to create an object

Prototype are properties in object objects, and all person objects can have prototype this property.

You can add some attributes to the object's prototype, methods.

The disadvantage of creating an object simply by using a prototype: ① cannot pass parameters and can only change its value after the object is created

② may cause program errors

Copy Code code as follows:

<! DOCTYPE html>
<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>
<body>
</body>

Copy Code code as follows:

<! DOCTYPE html>
<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>
<body>
</body>

Simply using a stereotype to define an object cannot be used to assign an initial value to a property in a constructor, only to change the property value after the object is generated.

The fifth way: Use the Prototype + constructor method to define the object----recommend the use of

Properties between objects do not interfere with each other
Share the same method between objects

Copy Code code as follows:

<! DOCTYPE html>
<script type= "Text/javascript" >
To define an object using the Prototype + constructor method
function person () {
property is defined in the constructor
This.username=new Array ();
This.password= "123";
}
method is defined in the prototype
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>
<body>
</body>

The sixth way: Dynamic prototyping mode----recommended use of

In a constructor, you let all objects share a method by the amount of flags, and each object has its own properties.

Copy Code code as follows:

<! DOCTYPE html>
<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>
<body>
</body>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.