从零开始的es62 参数 模块化

	// es6默认参数 当没有参数时选择默认参数
	function a(x,y=7,z=42) {
		return x+y+z;
	}
	console.log(a(1));  //50

	// ES6 可变参数不确定有几个参数
	function b(...a) {
		var sum = 0;
		a.forEach(item=>{
			sum+=item*1
		});
		return sum
	}
	console.log(b(1,2,3)); //9

	//es6合并数组
	var params = ['hello',true,7];
	var other = [
		1,2,...params
	];
	console.log(other); //1 2 helo true 7
// a.js
var sex="boy";
var echo=function(value){
  console.log(value)
}
export {sex,echo}  
//通过向大括号中添加sex,echo变量并且export输出,就可以将对应变量值以sex、echo变量标识符形式暴露给其他文件而被读取到
//不能写成export sex这样的方式,如果这样就相当于export "boy",外部文件就获取不到该文件的内部变量sex的值,因为没有对外输出变量接口,只是输出的字符串。
// b.js
import {sex,echo} from "./a.js" 
console.log(sex)   // boy
echo(sex) // boy

猜你喜欢

转载自blog.csdn.net/qq_38674970/article/details/82890271