2013-06-29 16 views
71

This recent video afferma che la destrutturazione di EMCAScript 6 è già parzialmente implementata in Node.JS. Ho provato vari esempi (utilizzando v0.10.12 e la bandiera --harmony) comeDistruzione in Node.JS

var [a, b] = [1, 2]; 

e

var {a: a, b: b} = {a: 1, b: 2}; 

inutilmente. This ticket sembra suggerire che la destrutturazione non è ancora supportata in V8.

La destrutturazione è stata parzialmente implementata in Node.JS? Quali sono i frammenti di codice con cui posso giocare?

risposta

85

Aggiornamento per il nodo v6 e successivi: Nodo v6 supporta l'assegnazione destrutturazione senza niente speciali:

var [a, b] = [1, 2]; 

Per le versioni precedenti di nodo: È possibile ottenere l'elenco delle caratteristiche di armonia supportate digitando:

node --v8-options | grep harmony

nodo 5.x vi darà:

--es_staging (enable all completed harmony features) 
--harmony (enable all completed harmony features) 
--harmony_shipping (enable all shipped harmony fetaures) 
--harmony_modules (enable "harmony modules" (in progress)) 
--harmony_regexps (enable "harmony regular expression extensions" (in progress)) 
--harmony_proxies (enable "harmony proxies" (in progress)) 
--harmony_sloppy_function (enable "harmony sloppy function block scoping" (in progress)) 
--harmony_sloppy_let (enable "harmony let in sloppy mode" (in progress)) 
--harmony_unicode_regexps (enable "harmony unicode regexps" (in progress)) 
--harmony_reflect (enable "harmony Reflect API" (in progress)) 
--harmony_destructuring (enable "harmony destructuring" (in progress)) 
--harmony_default_parameters (enable "harmony default parameters" (in progress)) 
--harmony_sharedarraybuffer (enable "harmony sharedarraybuffer" (in progress)) 
--harmony_atomics (enable "harmony atomics" (in progress)) 
--harmony_simd (enable "harmony simd" (in progress)) 
--harmony_array_includes (enable "harmony Array.prototype.includes") 
--harmony_tostring (enable "harmony toString") 
--harmony_concat_spreadable (enable "harmony isConcatSpreadable") 
--harmony_rest_parameters (enable "harmony rest parameters") 
--harmony_sloppy (enable "harmony features in sloppy mode") 
--harmony_arrow_functions (enable "harmony arrow functions") 
--harmony_new_target (enable "harmony new.target") 
--harmony_object_observe (enable "harmony Object.observe") 
--harmony_spreadcalls (enable "harmony spread-calls") 
--harmony_spread_arrays (enable "harmony spread in array literals") 
--harmony_object (enable "harmony Object methods") 

La bandiera richiesta, --harmony_destructuring, è stata aggiunta nel nodo 4.1. Attualmente, è necessario passare il flag --harmony_destructuring per abilitare la funzione:

$ node --harmony_destructuring 
> var {foo} = {foo: 'bar'}; 
undefined 
> foo 
'bar' 
+0

Strano che non funzioni ancora senza 'var'. –

+3

@BrianMcCutchon Senza 'var', dovrai fare' ({foo} = {foo: 'bar}) 'per impedire che l'iniziale' {'venga interpretato come l'avvio di un blocco. È lo stesso in qualsiasi implementazione. –

+0

@torazaburo Interessante, non ci aveva pensato. Dovresti essere in grado di omettere il 'var' /' let' durante la destrutturazione degli array, però, ma non sembra funzionare ancora nel nodo. –

10

Il ES6 compatibility table mostra che destrutturazione non è supportato in entrambi Chrome 45, o Nodo v4.

+1

Anche con 'node --harmony_destructuring' –

+7

Nel Nodo v5.0.0 funziona con' node --harmony_destructuring'. Non so di v4. – kernel

+6

funziona per me nel nodo 4.2.2 con --harmony_destructuring –

14

Il recentemente node.js v6 utilizza V8 versione 5.0 che è supporting 93% delle funzionalità del linguaggio ES2015 (anche il 96% in v6.1).

Gli incarichi di distruzione possono ora essere considerati stabili e possono essere utilizzati senza bandiere.