JavaScript code block
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Function factorial (num ){
If (num <= 1 ){
Return 1;
} Else {
Return num * arguments. callee (num-1 );
}
}
Var trueFactorial = factorial;
Factorial = function (){
Return 0;
}
Alert (trueFactorial (5); // 120
Alert (factorial (5); // 0
</Script>
C # code blockCopy codeThe Code is as follows: protected void Page_Load (object sender, EventArgs e)
{
TestFactorial ();
}
Public delegate int factorialDelegate (int num); // defines recursive function Delegation
Private void TestFactorial ()
{
FactorialDelegate fdelegate = factorial; // compare it with javascript Functions.
FactorialDelegate trueFactorial = fdelegate;
Fdelegate = returnZero;
Int num1 = trueFactorial (5); // 120.
Int num2 = fdelegate (5); // 0
}
Private int returnZero (int num)
{
Return 0;
}
Private int factorial (int num)
{
If (num <= 1)
{
Return 1;
}
Else
{
Return num * factorial (num-1 );
}
}
From the above, we can see that:
1. Functions in javascript do not need to be set to whether the function has a return value. In this case, the return value type of the function is of course unnecessary.
2. Functions in javascript are actually an object. The strong types of languages (C, C ++, and C #) We are exposed to are very different.
3. javascript contains an array class Object arguments, which contains all parameters in the input function. This object also has a property named callee, which is a pointer pointing to a function that owns this arguments object. Let's take a look at the C # code block. The execution of trusted factorial is tightly coupled with the function factorial. We cannot eliminate this close coupling phenomenon. In the javascript code block above, when the variable trueFactorial gets the value of factorial. Then, we simply assigned a function that returns 0 to the factorial variable. If arguments. callee is not used as originally, call trueFactorial () and 0 is returned. After the code in the function body is removed from the coupling state of the function name, trueFactorial () can still calculate the factorial normally. As for factorial (), it is only a function that returns 0.
Reference Book Javascript Advanced Programming
Some texts are from the above books