The processing method of different browser JavaScript variable scopes _javascript tips

Source: Internet
Author: User
Tags variable scope

1. About prototype: Here 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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


2. About variable scope, and Ie,firefox to JS different processing, here are a few examples, there are a few are originally seen from elsewhere in the notes, and some of my own dug out.
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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


2.3:

<script type= "Text/javascript" > Function Click_handler () {alert (this);//Popup Window object} </script> ... <button id= ' Thebutton ' onclick= ' Click_handler () ' >click me!</button>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


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); The function addhandler () {var deep_thought = new Bigcomputer (), The_button = document.getElementById (' Thebutton ') ; The_button.onclick = deep_thought.ask_question; } window.onload = addhandler; result [undefined] </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


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); The function addhandler () {var deep_thought = new Bigcomputer (), The_button = document.getElementById (' Thebutton ') ; The_button.onclick = deep_thought.ask_question; } window.onload = addhandler; result [</script>]
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


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 (o BJ, 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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


2.10

<script>//** variables need to be defined alert (document); [Object HTMLDocument] Alert (window.document); [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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]


2.11

<script type= "Text/javascript" > Function method () {var window={}; alert (window.location); alert (window.location); Method (); alert (window.location); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

2.12

<script type= "Text/javascript" > var window={};//error: illegal Assignment! This works in Ie,but throw a Exception in Firefox alert (window.location); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

2.13

<script type= "Text/javascript" >/** This is a very annoying code to disable alert/window.alert ("Hello World"); Window.alert=function (str) {document.write (str); }; Alert ("Hello World"); Window.alert ("Hello World"); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

2.14:

Notice how the results of these three HTML files are different.
A.html:
<script type= "Text/javascript" > Alert (sock); Function sock () {alert ("function sock executed!"); alert (sock); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

B.html:
<script type= "Text/javascript" > Alert (sock); var sock=function () {alert ("function sock executed!"); alert (sock); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

C.html:
<script type= "Text/javascript" >//here does not pop "undefined"//a bit eccentric ah! alert (sock); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

2.15

Look at the difference in how the Ie,firefox are run:

<script type= "Text/javascript" > Object.prototype.hello=function () {alert ("Hello"); } Window.hello (); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]

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.