1.函数的默认参数可以直接写在参数里面
function foo(height = 50, color = 'red')
{
// ...
}
2。模板字符串
不使用ES6
使用+号将变量拼接为字var name = 'Your name is ' + first + ' ' + last + '.'
ES6
将变量放在大括号之中:
var name = `Your name is ${first} ${last}.`
3.多行字符串放在反引`拼接
var roadPoem = `Then took the other, as just as fair,
And having perhaps the better claim
Because it was grassy and wanted wear,
Though as for that the passing there
Had worn them really about the same,`
不使用ES6
嵌套两个setTimeout回调函数:
setTimeout(function()
{
console.log('Hello'); // 1秒后输出"Hello"
setTimeout(function()
{
console.log('Fundebug'); // 2秒后输出"Fundebug"
}, 1000);
}, 1000);
使用ES6
使用两个then是异步编程串行化,避免了回调地狱:
var wait1000 = new Promise(function(resolve, reject)
{
setTimeout(resolve, 1000);
});
wait1000
.then(function()
{
console.log("Hello"); // 1秒后输出"Hello"
return wait1000;
})
.then(function()
{
console.log("Fundebug"); // 2秒后输出"Fundebug"
});
5.对象的方法
Object.keys()方法,获取对象的所有属性名或方法名(不包括原形的内容),返回一个数组。
var obj={name: "john", age: "21", getName: function () { alert(this.name)}};
console.log(Object.keys(obj)); // ["name", "age", "getName"]
console.log(Object.keys(obj).length); //3
console.log(Object.keys(["aa", "bb", "cc"])); //["0", "1", "2"]
console.log(Object.keys("abcdef")); //["0", "1", "2", "3", "4", "5"]
Object.assign (),assign方法将多个原对象的属性和方法都合并到了目标对象上面。可以接收多个参数,第一个参数是目标对象,后面的都是源对象。
给一个对象新增一个属性可以用该方法。
6.import和export
ES6标准中,JavaScript原生支持模块(module)了。这种将JS代码分割成不同功能的小块进行模块化,将不同功能的代码分别写在不同文件中,各模块只需导出公共接口部分,然后通过模块的导入的方式可以在其他地方使用。
export用于对外输出本模块(一个文件可以理解为一个模块)变量的接口。
import用于在一个模块中加载另一个含有export接口的模块。
import和export命令只能在模块的顶部,不能在代码块之中。
//导入部分
//全部导入
import Person from './example'
//将整个模块所有导出内容当做单一对象,用as起别名
import * as example from "./example.js"
console.log(example.name)
console.log(example.getName())
//导入部分
import { name } from './example'
//导出部分
// 导出默认
export default App
// 部分导出
export class User extend Component {};