简体   繁体   中英

Default Parameter Value in Arrow Function

I'm new to ES6 Javascript which means i'm exploring it. I like the arrow function and default parameter feature from ES6 which is mentioned in the below site.

http://es6-features.org/#ExpressionBodies
http://es6-features.org/#DefaultParameterValues

Below is my code snippet of ES6 and i have tested this in Chrome 47 . I'm trying to give default parameter value for my arrow function which is currently throwing error like

<script type="text/javascript">
  'use strict';
  var greet = (name = 'Venkat') => (console.log('Hi ' + name + '!'));
  greet(); // expected output: Venkat
  greet('Venkatraman'); // expected output: Venkatraman
</script>

Let me know whether its possible, if so, explain with solution and what i'm doing wrong here.

No, that's not possible (yet, I suppose). What you can do though:

var greet = name => console.log('Hi ' + (name || 'Venkat') + '!');
greet(); // output: Venkat
greet('Venkatraman'); // output: Venkatraman

Try it here

[ jan 2018 ] The default parameter value is now supported in all major browsers I suppose

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM