Comparison between Javascript and C # using factorial Functions

Source: Internet
Author: User

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 block
Copy 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

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.