Babel 入门

http://www.ruanyifeng.com/blo...

Babel packages

https://github.com/babel/babe...

谈谈Babel v6的新特性及未来定位

http://www.open-open.com/lib/...

Babel 在线转换

https://babeljs.io/repl/

Babel 使用指南

http://guoyongfeng.github.io/...

1. Babel core 浏览器环境

安装: npm install --save-dev babel-core@5

2. 命令行转码babel-cli

安装: npm install --save-dev babel-cli

改写package.json

{
  // ...
  "devDependencies": {
    "babel-cli": "^6.0.0"
  },
  "scripts": {
    "build": "babel src -d lib"
  },
}

转码的时候,就执行下面的命令。

npm run build

实例

package.json

{
  "name": "es6-sec",
  "version": "1.0.0",
  "description": "",
  "repository": "",
  "license": "",
  "readme": "",
  "scripts": {
    "build": "babel src -d asset"
  },
  "devDependencies": {
    "babel-cli": "^6.18.0",
    "babel-preset-es2015": "^6.18.0"
  }
}

.babelrc

{
  "presets": ["es2015"],
  "plugins": []
}

js

// 变量,常量
function test() {
    let a = 10;
    const b = 20;
    console.log(a);
    console.log(b);
}
test();

// 数组的解构赋值
function example() {
    return [1, 2, 3];
}
var [a, b, c] = example();
console.log('a:' + a);
console.log('b:' + b);
console.log('c:' + c);

// 对象的解构赋值
function examples() {
    return {
        foo: 1,
        baz: 2
    };
}
var {foo, baz} = examples();
console.log('foo:' + foo);
console.log(baz);

var input = [1, 2, 3];
input.map(item => item + 1);

html

<!DOCTYPE html>
<html lang="ch-CN">
<head>
    <meta charset="UTF-8">
    <title>es6-sec</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
</head>
<body>
<script type="text/javascript" src="asset/index.js"></script>
</script>
</body>
</html>

小渝人儿
1.1k 声望849 粉丝

前端工程师


引用和评论

0 条评论