標籤:rip on() ref 優先順序 const str 完全 系統內建 struct
測試一:
var fud01 = function()
{
var temp = 100;
this.temp = 200;
return temp + this.temp;
}
alert(typeof(fud01));
alert(fud01());
運行結果:
function 300
最普通的function使用方式,定一個JavaScript函數。在大擴號內的變數範圍中,this指代fud01的所有者。
測試二:
var fud02 = new function()
{
var temp = 100;
this.temp = 200;
return temp + this.temp;
}
alert(typeof(fud02));
alert(fud02.constructor());
運行結果: object 300
實際上這是定一個JavaScript中的使用者自訂對象,不過這裡是個匿名類。這個用法和函數本身的使用基本沒有任何關係,在大擴號中會構建一個變數範圍,this指代這個範圍本身。
測試三:
var fud3 = new Function(‘var temp = 100; this.temp = 200; return temp + this.temp;‘);
alert(typeof(fud3));
alert(fud3());
運行結果: function 300
使用系統內建函數對象來構建一個函數,這和方法一中的第一種方式在效果和初始化優先順序上都完全相同,就是函數體以字串形式給出。
測試四:
var fud4 = Function(‘var temp = 100; this.temp = 200; return temp + this.temp;‘);
alert(typeof(fud4));
alert(fud4());
運行結果: function 300
這個方式是不常使用的,效果和方法三一樣,不過不清楚不用new來產生有沒有什麼副作用。
詳解new function(){}和function(){}()
function,new function,Function,new Function 之間的區別