Define objects in javascript factory mode and javascript factory objects

Source: Internet
Author: User

Define objects in javascript factory mode and javascript factory objects

Each function object has a length attribute, indicating the number of parameters that the function expects to receive.

Copy codeThe Code is as follows:
<Html>
<Head>
<Script type = "text/javascript">
Var add = function (num1, num2, num3 ){
Alert (num1 + num2 + num3 );
}
Alert (add. length );
</Script>
</Head>
<Body>
</Body>
</Html>

About how to create js object-oriented,

Objectives:

Construct an order object.
Contains three attributes: date, amount, and submitter
Contains a method: Display string: "XX has submitted an order with a quota of: xxxx RMB in XXXX-XX-XX"

One factory Method

Copy codeThe Code is as follows:
<Script type = text/javascript>
/*
Factory method: return objects by using methods. new objects do not need to be generated during use.
*/
Function createOrder () // You can also construct a factory method with parameters to initialize object data based on input parameters.
{
Var order = new Object ();
Order. Date = "1990-1-1 ";
Order. Price = "3200 ";
Order. Name = "Vince Keny ";
Order. Show = function () // place the show method in the factory, and create a Show method for each instance. wasting resources is a disadvantage of this mode.
{
Alert (this. Name + "in" + this. Date + "submitted" + this. Price + "RMB order .")
}
Return order;
}
// Return objects in factory mode:
Var order = createOrder ();
// You can also use this method to convert the factory mode to "pseudo constructor". Because new is used in the factory, the new operator will be ignored when an object is created.
Var order2 = new createOrder ();
Order. Show ();
Order2.Show ();
</Script>

2. constructor Method

Copy codeThe Code is as follows:
/*
In constructor mode, the method declaration is the same as the factory method, and the same problem exists. It can also be extracted. The difference is that the attribute Declaration uses this
You must use the new operator to generate an instance.
*/
Function Order ()
{
This. Date = "1990-1-1 ";
This. Price = "3200 ";
This. Name = "Vince Keny ";
This. Show = function ()
{
Alert (this. Name + "in" + this. Date + "submitted" + this. Price + "RMB order .")
}
}
 
Var order = new Order ();
Order. Show ();

Tri-Prototype

Copy codeThe Code is as follows:
/*
Prototype: Use prototype
*/
Function Order ()
{}
 
Order. prototype. Date = "1990-1-1 ";
Order. prototype. Price = "3200 ";
Order. prototype. Name = "Vince Keny ";
Order. prototype. Show = function ()
{
Alert (this. Name + "in" + this. Date + "submitted" + this. Price + "RMB order .")
}
Var order = new Order ();
Order. Show ();

4. Mixed Constructors/prototype

Copy codeThe Code is as follows:
/*
Hybrid constructor/prototype: Initialize attribute fields using constructor and use prototype constructor to constructor.
*/
Function Order ()
{
This. Date = "1990-1-1 ";
This. Price = "3200 ";
This. Name = "Vince Keny ";
}
Order. prototype. Show = function ().
{
Alert (this. Name + "in" + this. Date + "submitted" + this. Price + "RMB order .")
}
Var order = new Order ();
Order. Show ();

Five dynamic hybrid modes

Copy codeThe Code is as follows:
/*
Dynamic mixing: the difference between the dynamic and mixed methods lies in the location of the declared method. Place the method life inside the constructor, which is more in line with the object-oriented method.
*/
Function Order ()
{
This. Date = "1990-1-1 ";
This. Price = "3200 ";
This. Name = "Vince Keny ";

If (typeof Order. _ initialized = "undefined ")
{
Order. prototype. Show = function ().
{
Alert (this. Name + "in" + this. Date + "submitted" + this. Price + "RMB order .")
};
Order. _ initialized = true;
}
}

Function Car (sColor, iDoors ){
Var oTempCar = new Object;
OTempCar. color = sColor;
OTempCar. doors = iDooes;
OTempCar. showColor = function (){
Alert (this. color)
};
Return oTempCar;
}
Var oCar1 = new Car ("red", 4 );
Var oCar2 = new Car ("blue", 3 );
OCar1.showColor (); // outputs "red"
OCar2.showColor (); // outputs "blue"

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.