<script type="text/javascript"> //A little knowledge about functions, wallimn function func(){ this.id='func'; } var a = func;//Assign the function corresponding to func to a, this function is not executed. After that, a() is equivalent to func(), that is, a and func point to the same function body. console.log(a)//The output is the function definition console.log(a.id);//The output is undefined console.log(window.id);//The output is undefined, because only the function assignment operation is performed, and the function is not executed var b = func();//Assign the execution result of the func function to b, and the func function does not return a value, then b is undefined. In this process, this in the func body refers to window. According to the output of window.id, it can be seen that console.log(b)//The output is undefined //console.log(b.id)//There will be an error in execution. console.log(window.id);//The output is func, when the function is called normally (without new), the internal this points to window var c = new func();//It is equivalent to executing func as a constructor, returning an object and assigning it to c. At this point, c.id can be called. This in the function body points to the context formed after the function is executed console.log(c);//The output is the created object, and the this pointer points to the function execution context. console.log(c.id);//out as func var d = new func (); d.id="new func"; console.log(c.id);//The output is func console.log(d.id);//The output is new func. Each time new, a separate context is obtained. </script>
Small knowledge of javascript functions
Functions in javascript are very different from functions in other languages, which makes people feel weird. Especially with or without parentheses and with or without new, the results are very different, and the this point also has unpredictable changes.
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=326695273&siteId=291194637
Ranking