回覆列表
  • 1 # Jcsssh

    document.body.onload=function(xxx) document.body.onload=new function(xxx) document.body.onload=function(){xxxxxxxxx} 首先第一種:樓主不要寫成function(xxx),否則大家都以為是function關鍵字,其實只是個自定義的函式。至於下面這種寫法效果是一樣的,但是注意了這裡有new關鍵字,而且不是function,而是Function,比如說:document.onmouseup=new Function("flag=true");第三種即最常見的一種,語法為:function(){statement}-----------------------------------一. var foo01 = function() { var temp = 100; this.temp = 200; return temp + this.temp; } 這裡過載了foo01函式,效果和function foo01(){statement}差不多,但是區別在於var foo01=function(){statement}重灌了foo01函式,也就相當於個模型,區別還是有的,比如說有個函式分別呼叫了這兩種方式寫的函式foo01,但是例子中的寫法將foo01的方法繼承了,但是function foo01(){}這種寫法則直接執行了函式,所以兩個寫法都很有用處。二.var foo02 = new function() { var temp = 100; this.temp = 200; return temp + this.temp; } 這種寫法不常見,但是它和例一差不多,只不過多了關鍵字new,很明顯該函式必須先定義自定義函式的模型,然後才能對此函式用new關鍵字來例項化。三.var foo3 = new Function(’var temp = 100; this.temp = 200; return temp + this.temp;’);在上面已經提及過了。

  • 中秋節和大豐收的關聯?
  • RNG擊敗IG,Uzi小虎完美髮揮過年,網友們發明了哪些有趣的評論呢?