Processing of javascript variable scopes in different browsers

Source: Internet
Author: User
Tags variable scope

1. About prototype: prototype is a feature of javascript, not the famous prototype framework:

<Script type = "text/javascript">
Var string = "hello world ";
Try {
Alert (string. phone ());
} Catch (e) {alert (e );}
String. prototype. phone = function ()
{
Return "159-10957151 ";
}

Alert (string. phone ());
</Script>

2. Regarding the variable scope, different processing methods for js from IE and firefox, there are several examples here, some of which are notes that were originally viewed elsewhere, some of which were mine by myself.

2.1

<Script type = "text/javascript">
Var deep_thought = {
The_answer: 42,
Ask_question: function (){
Return this. the_answer;
}
};

Var the_meaning = deep_thought.ask_question ();
Alert (the_meaning );
</Script>

2.2

<Script type = "text/javascript">
Function test_this (){
Return this;
}
Var I _wonder_what_this_is = test_this ();
Alert (I _wonder_what_this_is );
// Result: [object window];
</Script>

2.3:

<Script type = "text/javascript">
Function click_handler (){
Alert (this); // The window object is displayed.
}
</Script>
...
<Button id = 'thebutton 'onclick = 'click _ handler () '> click me! </Button>

2.4

<Script type = "text/javascript">
Function click_handler (obj ){
Alert (obj );
// Result: [object HTMLButtonElement]
}
</Script>
...
<Button id = 'thebutton 'onclick = 'click _ handler (this) '> click me! </Button>

2.5

<Button id = 'thebutton 'onclick = 'click _ handler (this) '> click me! </Button>
<Script type = "text/javascript">
Function BigComputer (answer ){
This. the_answer = answer;
This. ask_question = function (){
Alert (this. the_answer );
}
}

Function addhandler (){
Var deep_thought = new BigComputer (42 ),
The_button = document. getElementById ('thebutton ');
The_button.onclick = deep_thought.ask_question;
}
Window. onload = addhandler;
// Result [undefined]
</Script>
...

2.6

<Button id = 'thebutton 'onclick = 'click _ handler (this) '> click me! </Button>
<Script type = "text/javascript">
Function BigComputer (answer ){
Var self = this;
Self. the_answer = answer;
Self. ask_question = function (){
Alert (self. the_answer );
}
}

Function addhandler (){
Var deep_thought = new BigComputer (42 ),
The_button = document. getElementById ('thebutton ');
The_button.onclick = deep_thought.ask_question;
}
Window. onload = addhandler;
// Result [42]
</Script>
...

2.7

<Button id = 'thebutton 'onclick = 'click _ handler (this) '> click me! </Button>
<Script type = "text/javascript">
Function btn_click (){
Alert (this );
}

Function addhandler (){
The_button = document. getElementById ('thebutton ');
The_button.onclick = btn_click;
}

Window. onload = addhandler;

// Result [undefined]
</Script>
...

2.8

<Button id = 'thebutton 'onclick = 'click _ handler (this) '> click me! </Button>
<Script type = "text/javascript">
Function real_func ()
{
Alert (this );
}
Function btn_click (){
SetTimeout (real_func, 100 );
}

Function addhandler (){
The_button = document. getElementById ('thebutton ');
The_button.onclick = btn_click;
}

Window. onload = addhandler;

// Result [undefined]
</Script>
...

2.9

<Button id = 'thebutton 'onclick = 'click _ handler (this) '> click me! </Button>
<Script type = "text/javascript">
Function. prototype. bind = function (obj ){
Var method = this,
Temp = function (){
Return method. apply (obj, arguments );
};

Return temp;
}
Var real_func = function ()
{
Alert (this );
}
Function btn_click (){
SetTimeout (real_func.bind (this), 100 );
}
Function addhandler (){
The_button = document. getElementById ('thebutton ');
The_button.onclick = btn_click;
}
Window. onload = addhandler;
// Result [undefined]
</Script>
...

2.10

<Script>
// ** Variables need to be defined
Alert (document); // [object HTMLdocument]
Alert(registry.doc ument); // [object HTMLdocument]

Alert (window. face); // pretty
Var face = "pretty ";
Alert (face); // pretty
Alert (window. face); // pretty
Alert (window. sock); // undefined
Alert (sock); // ERROR: sock not defined
</Script>

2.11

<Script type = "text/javascript">
Function method ()
{
Var window = {};
Alert (window. location );
}
Alert (window. location );
Method ();
Alert (window. location );
</Script>

2.12

<Script type = "text/javascript">
Var window ={}; // ERROR: invalid value assignment!
// This works in IE, but throw an Exception in firefox
Alert (window. location );
</Script>

2.13

<Script type = "text/javascript">
/** This is a very annoying piece of code.
Alert can be disabled.
*/
Window. alert ("hello world ");
Window. alert = function (str ){
Document. write (str );
};
Alert ("hello world ");
Window. alert ("hello world ");
</Script>

2.14:

Note the differences between the results of these three html files.
A.html:
<Script type = "text/javascript">
Alert (sock );
Function sock ()
{
Alert ("function sock executed! ");
}
Alert (sock );
</Script>
B .html:
<Script type = "text/javascript">
Alert (sock );
Var sock = function ()
{
Alert ("function sock executed! ");
}
Alert (sock );
</Script>
C.html:
<Script type = "text/javascript">
// "Undefined" is not displayed here"
// A little eccentric!
Alert (sock );
</Script>

2.15

Let's take a look at the differences between IE and firefox:

<Script type = "text/javascript">
Object. prototype. hello = function (){
Alert ("hello ");
}
Window. hello ();
</Script>

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.