JavaScript code block
Copy Code code 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 block
Copy Code code as follows:
protected void Page_Load (object sender, EventArgs e)
{
Testfactorial ();
}
public delegate int factorialdelegate (int num); To define a recursive function delegate
private void Testfactorial ()
{
Factorialdelegate fdelegate = factorial; Note the comparison 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, you can see:
1, JavaScript in the function does not need to set the return value of the function, then the function of the return value type of course there is no need to set.
2, the function in JavaScript is actually an object, this we contact the strong type of language (c, C + +, C #) is very different.
3. There is a class array object arguments in JavaScript that contains all the arguments in the incoming function. And this object also has a property called Callee, which is a pointer to the function that owns the arguments object. Look at the C # code block, the execution of the delegate truefactorial and the function factorial tightly coupled together. We have no way to eliminate this tightly coupled phenomenon. And in the JavaScript code block above, when the variable truefactorial gets the factorial value. Then we simply assign a function that returns 0 to the factorial variable. If you do not use Arguments.callee as you originally did, calling Truefactorial () returns 0. The Truefactorial () is still able to compute the factorial correctly after the coupling state of the code in the function body with the function name is removed. As for factorial (), he is now just a function that returns 0.
Reference book "JavaScript Advanced Programming"
Part of the text from the above books