2013-10-04 22 views
0

在觀看http://www.youtube.com/watch?v=b0EF0VTs9Dc,我發現這樣的javascript代碼的Javascript 「...」 參數

function unit(value) { 
    var monad = Object.create(prototype); 
    monad.bind = function (func, args) { 
     return func.apply(undefined, 
         [value].concat(
          Array.prototype.slice.apply(args || []))); 
    } 
    return monad; 
} 

被改寫爲

function unit(value) { 
    var monad = Object.create(prototype); 
    monad.bind = function (func, args) { 
     return func(value, ...args); 
    } 
    return monad; 
} 

的不過後者並沒有在Chrome和Firefox中運行(我沒有嘗試IE)。這是JavaScript中的新東西,目前的瀏覽器還不支持?

+3

[其餘的力學參數(HTTPS://developer.mozilla .org/en-US/docs/Web/JavaScript/Reference/rest_parameters) - 適用於Firefox Aurora/Nightly。你也可以[聽](https://gpodder.net/podcast/a-minute-with-brendan/the-arguments-argument-2)或[watch](http://vimeo.com/27911873)Brendan Eich說說它 –

回答

1

這是JavaScript中的新東西,目前的瀏覽器還不支持?

是的,這是語言規範(ECMAScript 6)的下一個版本的一部分,它仍然是草案。

它只被Firefox支持,並且只在實驗上,如Dan Heberden發佈的link中所述。

可以在當前的JavaScript中實現其中的一部分。例如,爲了獲得傳遞給函數的所有額外的,未命名的參數:

function f(x) { 
    var args = Array.prototype.slice.call(arguments, f.length); 
    console.log(args); 
} 
f(1,2,3); // logs [2, 3] 

或者,通過他們對其他功能:

function f(someFunc) { 
    var args = Array.prototype.slice.call(arguments, f.length); 
    someFunc.apply(null, args); 
} 
f(function(a,b){ 
    console.log(a, b); 
}, 1, 2); // logs 1 2