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 !! */