Javascript trap window global object_javascript skills

Source: Internet
Author: User
Ff and ie6 run the same results. It doesn't seem to be a bug. Why? Note: When the object method accesses the attributes of its object | required | use this. (unlike java). function Test (){
This. name = 'test ';
Var name = 2;
This. show = function (){
Alert (name );
Alert (this. name); // display name
}
}
Var test = new Test (); // create an object through the constructor
Test. show (); // output 2 and 'test', indicating that this must be added when the object method accesses its attributes.

Function Test2 (){
This. name = 'test2 ';
This. show = function (){
Alert (name );
Alert (this. name );
}
}
Test (); // directly call Test ();
Var test2 = new Test2 ();
Test2.show (); // output Test and Test2. It's strange. What is the value of name, and how can it be 'test' or a bug?
Alert (name );
Window. show (); // output 2, test; how can we use the show function? Is it a bug?

// Output 2, Test; Test, Test2; Test; 2, Test in sequence

// Ff and ie6 run the same results. It does not seem to be a bug. Why?
// Note: When an object method accesses its object attributes | required | use this. (unlike java ).
// The entire page is | window | object by default. The defined function is the window object method by default.
// When a function is directly called, it is equivalent to calling a method through window., then this inside the method is naturally \
// Window object. this. name = 'test' indicates that an attribute is added to the window object.
// When the name is not defined in the local scope of the method and alert (name) is called, it is equivalent to calling alert (window. name );
/* Ah, the code is messy. This looks like a very serious trap !! */
Related Article

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.