Code 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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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 select all Note: If you need to introduce external Js, You need to refresh it to execute]
2.3:
<Script type = "text/javascript"> function click_handler () {alert (this); // pop-up window object} script... <button id = 'thebutton 'onclick = 'click _ handler () '> click me! </Button>
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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 select all Note: If you need to introduce external Js, You need to refresh it to execute]
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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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 select all Note: If you need to introduce external Js, You need to refresh it to execute]
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 select all Note: If you need to introduce external Js, You need to refresh it to execute]
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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
2.11
<Script type = "text/javascript"> function method () {var window ={}; alert (window. location);} alert (window. location); method (); alert (window. location); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
2.13
<Script type = "text/javascript">/** this is a very annoying piece of code. You can disable alert */window. alert ("hello world"); window. alert = function (str) {document. write (str) ;}; alert ("hello world"); window. alert ("hello world"); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
B .html:
<Script type = "text/javascript"> alert (sock); var sock = function () {alert ("function sock executed! ");} Alert (sock); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
C.html:
<Script type = "text/javascript"> // "undefined" is not displayed here. // It is a bit eccentric! Alert (sock); script
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]
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
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]