Javascript Trap Window Global Object _javascript Tips
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 from the constructor
Test.show ()//Output 2 and ' Test ', stating that the object method must add this when accessing its properties.
function Test2 () {
This.name= ' Test2 ';
This.show=function () {
alert (name);
alert (this.name);
}
}
Test ();//Direct call to test ();
var test2=new Test2 ();
Test2.show ()//Output test,test2, very strange Ah, name asked what is worth, and how can it be ' Test ', bug?
alert (name);
Window.show (); Output 2,test; How could there be a show function, a bug?
FF and IE6 are the same running results, it seems not a bug, that is why?
Note: When an object method accesses the properties of its object | must | (not the same as Java).
The entire page is the default |window| object, then the defined function, the default is the Window object's method.
Direct call function directly, the equivalent of through window. Call method, then the method inside of this is naturally
Window object, this.name= ' Test ' is a property added to the Window object.
If name is not defined in the method local scope and call alert (name), it is equivalent to calling alert (Window.name);
* * Ah, the code is confusing. This looks like a very serious trap!! */
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.