多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > web前端 > jscript > javascript變量作用域在不同瀏覽器的處理

javascript變量作用域在不同瀏覽器的處理

來源:程序員人生   發布時間:2014-02-13 15:59:05 閱讀次數:3489次

1.關于prototype:這里prototype是javascript的一個特性,不是那個有名的prototype框架:

<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.關于變量作用域,和IE,firefox對js的不同處理,這里有幾個例子,有幾個是原來從別處看到的記的筆記,有的是我自己挖掘出來的.

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); // 彈出 window 對象
  }
</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(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>

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:非法賦值!
   // this works in IE,but throw an Exception in firefox
   alert(window.location);
</script>

2.13

<script type="text/javascript">
  /** 這是一段很讓人惱火的代碼
  可以禁用alert
  */
  window.alert("hello world");
  window.alert=function(str){
      document.write(str);
  };
  alert("hello world");
  window.alert("hello world");
</script>

2.14:

注意這三個html文件的結果有什么不同.
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"
// 有點偏心啊!
alert(sock);
</script>

2.15

看看IE,firefox下分別運行有何不同:

<script type="text/javascript">
Object.prototype.hello=function(){
  alert("hello");
}
window.hello();
</script>

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: s级毛片| 一级秋霞理论 | 亚洲一区区 | 欧美性受xxxx白人性爽网站 | 成人无遮挡免费网站视频在线观看 | 日韩拍拍拍| 手机免费在线视频 | 成年人免费在线视频观看 | 日韩精品久久久毛片一区二区 | 好大好湿好硬顶到了好爽(双性) | 国产精品99久久久久久www | 国产成人久久综合二区 | 日韩国产欧美视频 | 欧美日韩xxx| 精品一区二区三区自拍图片区 | 在线观看视频一区二区 | 福利片在线观看 | 国产女人久久精品 | 欧美毛片在线观看 | 91porn国产在线观看 | 国产成人精品一区二区 | 亚洲欧美成aⅴ人在线观看 亚洲欧美成人 | 手机看片日韩日韩国产在线看 | 精品国产中文一级毛片在线看 | xx日本护士| 久久大香伊人中文字幕 | 在线视频h | 欧美乱乱| 青青青青久久精品国产一百度 | 国产v在线播放 | 欧美乱人伦中文在线观看不卡 | 亚洲国产人久久久成人精品网站 | 国产精品爱久久久久久久小 | 欧美成人精品第一区 | 手机在线一区二区三区 | 2018年国产成人精品视频 | 国产精品毛片在线更新 | japanhdⅹxxxhd日本 | 亚洲产国偷v产偷v自拍自拍 | 自拍偷拍视频网 | 精品少妇一区二区三区视频 |