JS advanced Written exam must be the problem

Source: Internet
Author: User

1. var name = ' world! ';
(function () {
if (typeof name = = = ' undefined ') {
var name = ' Jack ';
Console.log (' Goodbye ' + name);
} else {
Console.log (' Hello ' + name);
}
})();

2.var a = 10;
function fn () {
Console.log (a);
}

function Bar (f) {
var a = 20;
f ();
}
Bar (FN);

3.function sideffecting (ary) {
Ary[0] = ary[2];
}
function Bar (a,b,c) {
c = 10
sideffecting (arguments);
Return a + B + C;
}
Bar (1,1,1)
The result is 21, arguments is an object in the JavaScript variable.
So the arguments and local variables refer to the same content.

4.function say666 () {
var num = 666;
var sayalert = function () {
alert (num);
}
num++;
return sayalert;
}
var Sayalert = say666 ();
Sayalert ()
After executing say666 (), the say666 () closure internal variable will exist, and the inner variable of the inner function of the closure will not exist
The garbage collection mechanism of JavaScript does not reclaim the resources that say666 () consumes.
Because the execution of an intrinsic function of say666 () requires a dependency on a variable in say666 ().

5.function F1 () {
var n=999;
Nadd=function () {n+=1}
function F2 () {
alert (n);
}
return F2;
}
var result=f1 ();
Result (); 999
Nadd ();
Result (); 1000
The first thing to say is that closures are the core concept inside functional language.
When a higher order nesting function is present, the compiler does the closure convention closure transformation, the core of which is that the variable is not allocated on the stack, but is allocated on the heap. That's why F1 has returned, but N can still be +1.
The landlord gives this program, is actually a high-order nesting function.
1. Because there are defined functions inside the function, this is nested. Pascal also allows nested functions.
2. The reason for the higher order is that functions can be called parameters passed and returned, like the C language we are familiar with.
But when higher order and nesting occur simultaneously, it can cause trouble, so both Pascal and C can only support one of them.
Let me analyze the execution flow of this program.
1. var result=f1 (); A function f2 is returned, so result is F2. This high-order function features a reference to the C-language function pointer.
2. result (); Call F2, apparently output 999.
3. Nadd (); It is important to note that this nadd is actually a lambda at the time of definition, an anonymous function, and the function is n+=1. This function is assigned to NADD when defined. So at this point, the n+=1 is actually called. Why can I find n? Because n is inside the heap.
4. Result (); Call F2, apparently output 1000.
Finally, how n is destroyed on the heap, this job is the garbage collector responsible. When n is not referenced by an env in any closure, it is recycled.
Function F1 () {
var n=999;
function F2 () {
alert (n);
}
Nadd=function () {n+=1}
return F2;
}
var result=f1 ();
Result (); 999
Nadd ();
Result (); 1000
var result=f1 (): F1 function returns the F2 function
Assigns the returned F2 function to the result global variable, (F2 's scope chain is saved to the result global variable)
Result (): Call result (), which forms a closure: A variable that has access to another function scope
Because the scope in F2 refers to the local variable of n in F1, when the F1 executes, the garbage collection mechanism finds that the n variable is still referenced by result, so the garbage collection mechanism does not release the N recovery.
So that n is kept in the result scope chain. The scope chain of result is normally able to access the local variable N in F1 to form a closure.
Nadd (): Nadd does not write Var so nadd is a global variable, in the call Nadd () and result () is the same as the closure, anonymous function functions () {n+=1} in the scope chain has n this local variable, so when the Nadd=funtion () {N+=1}, the scope chain of this anonymous function is saved to the global variable Nadd to form a closure, and the F1 local variable n=999,n+1=1000 is found in the call to the Nadd () scope chain.
Result (): result () on output 1000
Nadd (); Repeat the third step n+1 = 1001
Result (); Repeat fourth step output n
F1 (); When calling F1, there is no closure, n is always 999, because N is destroyed by the garbage collection mechanism every time the F1 execution ends, so the Var n=999 is called again, and the output is 999.
Why does the Nadd expression affect the N of F2?
Because N has not been destroyed because of the closure, Nadd () has also formed a closure and changed the value of N, so the subsequent call to result () n is not destroyed the recycle has been + 1, so it will be affected.
Finally, when the call to F1 () is not closed, the previous n is destroyed. So always output a=999;

6.function Fun (n,e) {
Console.log (e)
return {
Fun:function (m) {
Return fun (m,n);
}
};
}
var a = fun (0); A.fun (1); A.fun (2); A.fun (3);//undefined,?,?,?
var B = Fun (0). Fun (1). Fun (2). Fun (3);//undefined,?,?,?
var c = Fun (0). Fun (1); C.fun (2); C.fun (3);//undefined,?,?,?
Q: What are the outputs of the three line a,b,c?
Answer:
a:undefined,0,0,0
b:undefined,0,1,2
c:undefined,0,1,1
The first fun function, which belongs to the standard named function declaration, is a newly created function whose return value is an object literal expression, which belongs to a new type of object.
This new object contains a property called fun, which, through the above introduction, is an anonymous function expression, that is, the fun property is a newly created anonymous function expression.
Note: All declared anonymous functions are a new function.
So the first fun function is not the same as the second fun function, which is the newly created function.

JS advanced Written exam must be the problem

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.