Each function object has a length property that represents the number of parameters that the function expects to receive.
Copy Code code as follows:
<script type= "Text/javascript" >
var add =function (num1,num2,num3) {
alert (NUM1+NUM2+NUM3);
}
alert (add.length);
</script>
<body>
</body>
About the JS object-oriented creation method,
Goal:
Constructs an order object.
Contains three attributes: Date, amount, submitter
Contains a method: Display string: "XX submitted the amount in XXXX-XX-XX: xxxx order"
One factory way
Copy Code code as follows:
<script type=text/javascript>
/*
Factory approach: Returns an object by using a method that does not require a new object to be generated when used.
*/
function Createorder ()//You can also build a factory method with parameters to initialize the object data based on the parameters passed in.
{
var order = new Object ();
Order. Date = "1990-1-1";
Order. Price = "3200";
Order. Name = "Vince Keny";
Order. Show = function ()//The Show method is placed in the factory, and a show method is created for each instance. Wasting resources is the disadvantage of this model.
{
Alert (this. Name + "on" + this. Date + "The amount is submitted" + this. Price + "Yuan's order.")
}
return order;
}
To return an object using Factory mode:
var order = Createorder ();
You can also use this to transform the factory pattern into a "pseudo constructor", because new is used in the factory, so the new operator will be ignored when the object is created.
var order2 = new Createorder ();
Order. Show ();
Order2. Show ();
</script>
The way of two constructor functions
Copy Code code as follows:
/*
constructor, the declaration of the method is the same as that of the factory, and the same problem can be extracted. The difference is that the Declaration property uses this
And you need to use the new operator to generate the instance.
*/
function order ()
{
This. Date = "1990-1-1";
This. Price = "3200";
This. Name = "Vince Keny";
This. Show = function ()
{
Alert (this. Name + "on" + this. Date + "The amount is submitted" + this. Price + "Yuan's order.")
}
}
var order = New Order ();
Order. Show ();
Three-way prototype
Copy Code code as follows:
/*
Prototype mode: Using 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 + "on" + this. Date + "The amount is submitted" + this. Price + "Yuan's order.")
}
var order = New Order ();
Order. Show ();
Four hybrid constructor/prototype mode
Copy Code code as follows:
/*
Mixed constructors/Prototypes: Initialize property fields using constructors, and construct methods using prototypes.
*/
function order ()
{
This. Date = "1990-1-1";
This. Price = "3200";
This. Name = "Vince Keny";
}
Order.prototype.Show = function ().
{
Alert (this. Name + "on" + this. Date + "The amount is submitted" + this. Price + "Yuan's order.")
}
var order = New Order ();
Order. Show ();
Five Dynamic blending modes
Copy Code code as follows:
/*
Dynamic blending: And blending methods differ in where the method is declared. Put the method life inside the constructor, more in line with the object-oriented.
*/
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 + "on" + this. Date + "The amount is submitted" + this. Price + "Yuan's 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."