Comparison between JavaScript and C # from the factorial function _javascript techniques

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.