Today I read a book called "jquery practice" and ran to the final Appendix (JavaScript is required). After reading it, I found that I had a big misunderstanding about JavaScript, because I used Java and C #, both of which have less or less influence on my thinking, and the magic of grammar interferes with my understanding, let everyone better understand.
JavaScript Functions are considered objects. They are of the same type as other JavaScript objects, such as string, number, and date. Functions are also defined by the Javascript constructor. Therefore, a function is also called a first-class object. For example, we usually write this statement, Function dosomethingwonderful () {<br/> alert ('Does something wonderful '); <br/>} However, we can help you understand the above, Dosomethingwonderful = function () {<br/> alert ('Does something wonderful '); <br/>} ThisIt means to assign the function to the top-level variable (the property of window ).
Let's take a deeper look and introduce this into our explanation, VaR O1 = {handle: '01'}; <br/> var O2 = {handle: '02'}; <br/> var O3 = {handle: '03'}; <br/> window. handle = 'window'; <br/> function whoami () {<br/> return this. handle; <br/>}< br/> o1.identifyme = whoami; <br/> alert (whoami (); <br/> alert (o1.identifyme ()); <br/> alert (whoami. call (O2); <br/> alert (whoami. apply (O3 ));
The answer is what I want you to guess (window, O1, O2, O3). Here we should note what this refers to in the context of the function. Call call () and apply () to make the function look like an O2 method, but in fact this function is not associated with O2 (even the property of O2 is not ).