Javascript notes-several methods for defining objects

Source: Internet
Author: User

Several Methods for defining objects in JavaScript (JavaScript does not have the concept of classes, but only objects ):


1) Expand its attributes and methods based on existing objects:

VaR object = new object ();

Object. Name = "zhangsan ";
Object. sayname = function (name)
{
This. Name = Name;
Alert (this. Name );
}

Object. sayname ("Lisi ");

2) Factory method

// Create an object in factory Mode

Function Createobject ()
{
VaR object = new object ();

Object. Username = "zhangsan ";
Object. Password = "123 ";

Object. Get = function ()
{
Alert (this. username + "," + this. Password );
}

Return object;
}

VaR object1 = Createobject ();
VaR object2 = Createobject ();

Object1.get ();


Construction Method with Parameters:


Function Createobject (username, password)
{
VaR object = new object ();

Object. Username = username;
Object. Password = password;

Object. Get = function ()
{
Alert (this. username + "," + this. Password );
}

Return object;
}

VaR object1 = Createobject ("zhangsan", "123 ");
Object1.get ();


Share a function object with multiple objects, rather than having one function object for each object.

FUnction get ()
{
Alert (this. username + "," + this. Password );
}

Function Createobject (username, password)
{
VaR object = new object ();

Object. Username = username;
Object. Password = password;

Object. Get = get;

Return object;
}

VaR object = Createobject ("zhangsan", "123 ");
VaR object2 = Createobject ("Lisi", "456 ");

Object. Get ();
Object2.get ();


3) constructor Method


Function person ()
{
// Before executing the first line of code, the JS engine will generate an object for us
This. Username = "zhangsan ";
This. Password = "123 ";

This. getinfo = function ()
{
Alert (this. username + "," + this. Password );
}

// There is a hidden return statement used to return the previously generated object.
}

VaR person = new person ();
Person. getinfo ();


You can pass parameters when constructing objects.


Function person (username, password)
{
This. Username = username;
This. Password = password;

This. getinfo = function ()
{
Alert (this. username + "," + this. Password );
}
}

VaR person = new person ("zhangsan", "123 ");
Person. getinfo ();

4) Prototype

// Create an object using prototype
Function person ()
{

}

Person. Prototype. Username = "zhangsan ";
Person. Prototype. Password = "123 ";

Person. Prototype. getinfo = function ()
{
Alert (this. username + "," + this. Password );
}

VaR person = new person ();
VaR person2 = new person ();

Person. Username = "Lisi ";

Person. getinfo ();
Person2.getinfo ();

*****************

Function person ()
{

}

Person. Prototype. Username = new array ();
Person. Prototype. Password = "123 ";

Person. Prototype. getinfo = function ()
{
Alert (this. username + "," + this. Password );
}

VaR person = new person ();
VaR person2 = new person ();

Person. username. Push ("zhangsan ");
Person. username. Push ("Lisi ");
Person. Password = "456 ";

Person. getinfo ();

Person2.getinfo ();


If a prototype object is used, all generated objects share the attributes in the prototype,

Such an object changes this attribute and is also reflected in other objects.


An object defined by prototype alone cannot assign an initial value to the attribute in the constructor. The attribute value can only be changed after the object is generated.
Define objects using prototype + constructor. Attributes of objects do not interfere with each other. Each object shares the same method.


// Define objects using prototype + Constructor

Function person ()
{
This. Username = new array ();
This. Password = "123 ";
}

Person. Prototype. getinfo = function ()
{
Alert (this. username + "," + this. Password );
}

VaR P = new person ();
VaR P2 = new person ();

P. username. Push ("zhangsan ");
P2.username. Push ("Lisi ");

P. getinfo ();
P2.getinfo ();


5) Dynamic Prototype: In the constructor, all objects share a method with the flag, and each object has its own attributes.



Function person ()
{
This. Username = "zhangsan ";
This. Password = "123 ";

If (typeof person. Flag = "undefined ")
{
Alert ("invoked ");

Person. Prototype. getinfo = function ()
{
Alert (this. username + "," + this. Password );
}

Person. Flag = true;
}
}

VaR P = new person ();
VaR P2 = new person ();

P. getinfo ();

P2.getinfo ();



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.