[ES6] 07. Default Value for function param

Normally, we can set default value for function param:

//Here use "Hello" as default param
var receive =function(message="Hello", handle){
    handler(message);
}

receive("Come", function(message){
   console.log(message + ", "+ "John");
});

What we can do is use function as a default param:

var receive =function(message="Hello", handler=function(message){
    console.log(message + ", "+ "John");
}){
    handler(message);
}

receive("Come");  //Come, John

Then we can use => to refactor the code:

var receive =function(message="Hello", handler= message => console.log(message + ", "+ "John")){
    handler(message);
}

receive("Go");  //Go, John

It will be crazy: (do not use this, cannot be understood)

let receive = (message="Hello", handler= message => console.log(message + ", "+ "John")) => handler(message)

receive(); //Hello John
时间: 2024-12-29 10:44:45

[ES6] 07. Default Value for function param的相关文章

simulink error:Error in default port dimensions function of S-function ‘XXXXXXXXXXX’. This function does not fully set the dimensions of output port 2.

错误提示: Error in default port dimensions function of S-function 'XXXXXXXXXXX'. This function does not fully set the dimensions of output port 2. 问题描述:将m函数添加到simulink中的MATLAB function中后,运行后显示'XXXXXXXXXXXXX',这个模块的端口配置错误. 解决方法:双击MATLAB function模块,进入编程界面(如

es6 模块编译 *** is not function

今天学习vuejs,里面用到了es6的写法,遇到了一个很怪的问题,不知道有人遇到么. 安装的模块引用:import Vue from 'vue';(注意,Vue处没有{},如果加上这个就报错Uncaught TypeError: _vue.Vue is not a function) 自己写的模块,route-config.js: export function routeConfig() { console.log(3);} 引用自己写的模块:import { routeConfig } fr

ES6 new syntax of Arrow Function

Arrow Function.md Arrow Functions The basic syntax of an arrow function is as follows var fn = data => data; The code means : var fn = function( data ) { return data; } let getNumber = () => 42; console.log(typeof getNumber); console.log(getNumber()

[ES6] Function Params

1. Default Value of function param: The function displayTopicsPreview() raises an error on the very first line when called with no arguments. Let's fix that! function displayTopicsPreview( topics ){ var message = "There are currently " + topics.

ES6 中 export ,export default 区别

1.export与export default均可用于导出常量.函数.文件.模块等: 2.你可以在其它文件或模块中通过import+(常量 | 函数 | 文件 | 模块)名的方式,将其导入,以便能够对其进行使用: 3.在一个文件或模块中,export.import可以有多个,export default仅有一个: 4.通过export方式导出,在导入时要加{ },export default则不需要. export //a.js export const str = "hello es6&quo

[ES6] 06. Arrow Function =>

ES6 arrow function is somehow like CoffeeScirpt. CoffeeScript: //function call coffee = -> coffee() coffee=(message) -> coffee("Yo"), coffee "Yo" coffee=(message, other) -> coffee("Yo", 2), coffee "Yo", 2 N

ES6中表达export default const是无效的

问题 如果您是ES6新手,可以参考一下本文--高手请移驾别往!请先看下面的图形描述: 也就是说,ES6中default后面是不允许跟const关键字的. 分析 上图中表达可以更换成另一种形式,就可以了,如下所示: const decreaseAction={type:'decrease'} export default decreaseAction 引用 1,https://segmentfault.com/q/10100000101260102,https://blog.csdn.net/zh

React/React Native 的ES5 ES6写法对照表

转载: http://bbs.reactnative.cn/topic/15/react-react-native-%E7%9A%84es5-es6%E5%86%99%E6%B3%95%E5%AF%B9%E7%85%A7%E8%A1%A8 英文版: https://babeljs.io/blog/2015/06/07/react-on-es6-plus 很多React/React Native的初学者都被ES6的问题迷惑:各路大神都建议我们直接学习ES6的语法(class Foo extends

【学习笔记】ES6标准入门

这里简要记录一下对自己感触比较深的几个知识点,将核心的应用投放于实际的项目之中,提供代码的可维护性. 一.let和const { // let声明的变量只在let命令所在的代码块内有效 let a = 1; var b = 2; } console.log(a); // 报错: ReferenceError: a is not defined console.log(b); // for循环的技术器就很适合let命令 for (let i = 0; i < 3; i++) { console.l