Each function object has a length attribute, indicating the number of parameters that the function expects to receive.
The code copy 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 the creation of object-oriented js,
Target:
Build an order object.
Contains three attributes: date, amount, submitter
Contains a method: display string: "XX submitted an order with a quota of: XXXX yuan in XXX-XX-XX"
One factory method
The code copy is as follows:
<script type=text/javascript>
/*
Factory method: Return objects by using methods, there is no need to generate new objects through new when using them.
*/
function createOrder()//You can also build a factory method with parameters to initialize object data based on the passed parameters.
{
var order = new Object();
order.Date = "1990-1-1";
order.Price = "3200";
order.Name = "Vince Keny";
order.Show = function()//If you place the show method in the factory, you will create a Show method for each instance separately. Wasting resources is the disadvantage of this pattern.
{
alert(this.Name + " in " + this.Date + " Order with a quota of " + this.Price + " RMB.")
}
return order;
}
//Return the object using factory mode:
var order = createOrder();
//It can also be used in this way, transforming the factory pattern into a "pseudo-constructor". Because new is used in the factory, the new operator when creating the object will be ignored.
var order2 = new createOrder();
order.Show();
order2.Show();
</script>
Two constructor method
The code copy is as follows:
/*
The constructor method and method declaration are the same as the factory method, and there are the same problems and can be extracted. The difference is that this is used to declare attributes.
And you need to 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 + " Order with a quota of " + this.Price + " RMB.")
}
}
var order = new Order();
order.Show();
Three prototype methods
The code copy is as follows:
/*
Prototype method: 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 + " Order with a quota of " + this.Price + " RMB.")
}
var order = new Order();
order.Show();
Four hybrid constructor/prototype method
The code copy is as follows:
/*
Hybrid constructor/prototype method: Initialize attribute fields using constructor method and use prototype method to construct methods.
*/
function Order()
{
this.Date = "1990-1-1";
this.Price = "3200";
this.Name = "Vince Keny";
}
Order.prototype.Show = function().
{
alert(this.Name + " in " + this.Date + " Order with a quota of " + this.Price + " RMB.")
}
var order = new Order();
order.Show();
Five dynamic hybrid methods
The code copy is as follows:
/*
Dynamic mixing method: The difference between mixing method lies in the position of declaring the method. Putting the life of the method inside the constructor is more in line with 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 + " in " + this.Date + " Order with a quota of " + this.Price + " RMB.")
};
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"