Author: Truly
Date: 2007.7.29
Last time we discussed object declaration, member declaration, global variables, local variables, and namespace processing in "using object-oriented in JavaScript. This article continues to discuss object-oriented.Method overload.
At the same time, someone last asked about the function object'sApplyMethod andCallThe differences between methods will be discussed in this article.
First of all, we need to know that JavaScript has many language features that make it impossible to fully program based on previous programming experience, such as method overloading. In the previous articleArticle, We have already introduced that the variable with the same name in Javascript will overwrite the previous one, so it is not feasible to directly declare the overload method. However, the charm of JavaScript lies in its flexibility. Therefore, all expectations must be answered.
To save time, I willCodeThe Code is as follows:
< Script Type = 'Text / Javascript' >
Function ABC ()
{
This . Member = 1 ;
This . Method = Show;
}
Function Show (A, B, C)
{
This . Member = 123 ;
If (Arguments. Length = 1 )
If (Arguments [ 0 ]. Constructor = Number)
Showdomainint.apply ( This , Arguments );
Else If (Arguments [ 0 ]. Constructor = Date)
Show1_date.apply ( This , Arguments );
Else
Showdomainstring.apply ( This , Arguments );
If (Arguments. Length = 2 )
If (Arguments [ 0 ]. Constructor = Array)
Show3.apply ( This , Arguments );
Else
Show2.apply ( This , Arguments );
}
Function Show1_int ()
{
Alert (' 1 Integer input parameter' + A + 'Methode is called ');
}
Function Showevery string ()
{
Alert (' 1 String input parameter' + A + 'Methode is called ');
}
Function Show1_date ()
{
Alert (' 1 Date input parameter' + A + 'Methode is called ');
}
Function Show2 (A, B)
{
Alert (' 2 Input parameter is transfer: \ na :' + A + '\ NB :' + B + '\ Nthe member is' + This . Member );
}
// Demonstrate how to pass a reference
Function Show3 (A, B)
{
Arguments [ 0 ] [ 0 ] = 2 ;
Alert (' 2 Input parameter is transfer: \ na :' + A + '\ NB :' + B + '\ Nthe member is' + This . Member );
}
VaR O = New ABC ();
VaR Arr = New Array ();
Arr [ 0 ] = 321 ;
O. Method (ARR [ 0 ], 2 ); // Value Transfer
Alert (ARR [ 0 ]); // Original Value not changed
O. Method (ARR, 2 ); // Transfer by reference
Alert (ARR [ 0 ]); // Method modification during method call
O. Method ( New Date ());
</Script>
We cannot directly define overload methods, but we can define a public overload method call for other functions. This method is transparent for callers. You only need to call the method name and input appropriate parameters, just like in most advanced languages, you cannot define overload methods with the same input parameters and different return types.
Next, let's analyzeCallAndApplyThe difference is that all functions have these two methods, and they are not very different in use. The main difference is that the input parameters are different:
CallVariable Parameter sequences are used for value transfer, whileApplyArray orArgumentsObject.
Call method Syntax:
Call ([thisobj [, arg1 [, arg2 [, [,. argn])
Parameters
Thisobj
Optional. Will be used as the object of the current object.
Arg1, arg2, and argn
Optional. The method parameter sequence will be passed.
Description
The call method can be used to call a method instead of another object. The call method can change the object context of a function from the initial context to the new object specified by thisobj.
If the thisobj parameter is not provided, the global object is used as thisobj.
Syntax of the apply method:
Apply ([thisobj [, argarray])
Parameters
Thisobj
Optional. Will be used as the object of the current object.
Argarray
Optional. Array of parameters that will be passed to the function.
Description
If argarray is not a valid array or an arguments object, a typeerror occurs.
If neither argarray nor thisobj is provided, the global object will be used as thisobj and cannot be passed with any parameters.
Note: The above materials are from the JScript reference manual.
Section: Through the show2 method, we can see that the call or apply method can pass the current object to the method to access some members of the current object. This allows us to define some methods and call different custom classes. For more information, see the inheritance structure we defined in the previous article.
At the same time, the example above demonstrates that when the two parameters are passed, the parameter transmission method varies according to different parameters. If it is a value variable, the value is passed; otherwise, the value is passed as a reference.
Conclusion: When using JavaScript, you must understand and master some of the features of JavaScript.Programming LanguageThere are some differences. Its flexibility brings great flexibility to our development. Therefore, once you go deep into JavaScript programming, I promise you will love it for no reason.