JavaScript class and inheritance: this property

Source: Internet
Author: User

This attribute indicates the current object. If this is used in the global scope, it indicates the current page Object window. If this is used in the function, this indicates what is called Based on the object on which the function is called at runtime. We can also use the apply and call global methods to change the specific point of this in the function.

First, let's look at an example of using this in the global scope:

 
 
  1. < script type="text/javascript"> 
  2.     console.log(this === window);  // true  
  3.     console.log(window.alert === this.alert);  // true  
  4.     console.log(this.parseInt("021", 10));  // 10  
  5. < /script> 

The this attribute in a function is determined at runtime rather than at function definition, as follows:

 
 
  1. // Define a global function 
  2. Function foo (){
  3. Console. log (This. Fruit );
  4. }
  5. // Define a global variable, which is equivalent to window. fruit = "apple "; 
  6. Var fruit ="Apple";
  7. // In this case, the function foo points to the window object. 
  8. // This call method is equivalent to window. foo (); 
  9. Foo ();// "Apple" 
  10.  
  11. // Customize an object and direct its property foo to the global function foo 
  12. Var pack = {
  13. Fruit:"Orange",
  14. Foo: foo
  15. };
  16. // In this case, the function foo points to the window. pack object. 
  17. Pack. foo ();// "Orange" 
  18.  

The global functions apply and call can be used to change the point of this attribute in the function, as follows:

 
 
  1. // Define a global function 
  2. Function foo (){
  3. Console. log (This. Fruit );
  4. }
  5.  // Define a global variable 
  6. Var fruit ="Apple";
  7.  // Customize an object 
  8. Var pack = {
  9. Fruit:"Orange" 
  10. };
  11.  // Equivalent to window. foo (); 
  12. Foo. apply (window );// "Apple" 
  13.  // This = pack in foo 
  14. Foo. apply (pack );// "Orange" 

Note: The apply and call functions have the same functions. The only difference is that the parameter definitions of the two functions are different.

Because functions in JavaScript are also objects, we can see the following interesting example:

 
 
  1. // Define a global function 
  2. Function foo (){
  3. If(This=== Window ){
  4. Console. log ("This is window .");
  5. }
  6. }
  7.  
  8. // The function foo is also an object, so the boo attribute of foo can be defined as a function. 
  9. Foo. boo = function (){
  10. If(This=== Foo ){
  11. Console. log ("This is foo .");
  12. }Else If(This=== Window ){
  13. Console. log ("This is window .");
  14. }
  15. };
  16. // Equivalent to window. foo (); 
  17. Foo ();// This is window. 
  18.  
  19. // You can see this in the function pointing to the object that calls the function. 
  20. Foo. boo ();// This is foo. 
  21.  
  22. // Use apply to change the point of this in the function 
  23. Foo. boo. apply (window );// This is window. 
  1. Summary of three + 1 implementation methods of ExtJS Grid Tooltip
  2. Implementation of JavaScript asynchronous call framework chain
  3. JQuery-style chained call of JavaScript asynchronous call framework
  4. Code Implementation of the JavaScript asynchronous call framework
  5. JavaScript asynchronous call framework case design

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.