1.
Use this.name (variable name) in a function to assign a value, and when the function is called as a normal function , it can be accessed directly outside the function, because this point here points to the window:
function fn () { var r = 2; THIS.SS = 0; This.sum=function (num1,num2) { return (num1+num2) *r;}; } fn (); var B = new fn (); Console.log (ss); 0 Console.log (window.sum); 6 console.log (sum); 6
when a function is called as a method function, the situation is different, when this point changes and points to itself:
function fn () { var r = 2; THIS.SS = 0; This.sum=function (num1,num2) { return (num1+num2) *r;}; } var B = new FN ();/* Console.log (ss); Error Error Console.log (window.sum); Error error console.log (sum); Error Error * //can access Console.log (B.SS) through the instance; 0 Console.log (b.sum); 6
2.
var A; function fn2 () { var b = 0; return b; }; A = Fn2 (); Console.log (a); 0
JS Global Access Local variables