Javascript trap window Global Object

Source: Internet
Author: User

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 both have the same running result. 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 the default | window | object. The defined function is the window object method by default.
// directly calling a function is equivalent to using window. to call a method, this inside the method is naturally the \
// window object, this. name = 'test' adds an attribute to the window object.
// when no name is defined in the local scope of the method and alert (name) is called, it is equivalent to calling alert (window. name);
/*, Code is messy. This looks like a very serious trap !! */

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.