The only limits in our life are those we impose on ourselves.
注意是在浏览器,怎么弱法?简单的说:
几乎是语法糖,
async function
这些很有用的东西要依赖一个巨型Polyfill
才可使用。
ES6特性 | 兼容性 |
---|---|
箭头函数 | 支持 |
类的声明和继承 | 部分支持,IE8不支持 |
增强的对象字面量 | 支持 |
字符串模板 | 支持 |
解构 | 支持,但注意使用方式 |
参数默认值,不定参数,拓展参数 | 支持 |
let与const | 支持 |
for of | IE不支持 |
iterator, generator | 不支持 |
模块 module、Proxies、Symbol | 不支持 |
Map,Set 和 WeakMap,WeakSet | 不支持 |
Promises、Math,Number,String,Object 的新API | 不支持 |
export & import | 支持 |
生成器函数 | 不支持 |
数组拷贝 | 支持 |
详情请参考 ouven 的文章 Babel下的ES6兼容性与规范。
下面举些例子,大家可以对照上面的文章看看。
var add = (a, b) => a + b;
var add = function (a, b) { return a + b; };
var human = {
breathe() {
console.log('breathing...');
}
};
var human = {
breathe: function () {
console.log('breathing...');
}
};
const tmpl = `
<div class="content">
<h1>这是换行了。</h1>
</div>`;
function sayHi(name) {
return `How are you, ${name}?`;
}
var tmpl = "\n<div class=\"content\">\n <h1>\u8FD9\u662F\u6362\u884C\u4E86\u3002</h1>\n</div>";
function sayHi(name) {
return "How are you, " + name + "?";
}
var [name, gender, age] = ['wayou', 'male', 'secrect'];
function getArr() {
return [1, 2, 3];
}
var [a, b, c] = (() => [1, 2, 3])();
var { one, two, three } = (() => {
const one = 1, two = 2, three = 3;
return { one, three, two };
})();
var _a = ['wayou', 'male', 'secrect'], name = _a[0], gender = _a[1], age = _a[2];
function getArr() {
return [1, 2, 3];
}
var _b = (function () { return [1, 2, 3]; })(), a = _b[0], b = _b[1], c = _b[2];
var _c = (function () {
var one = 1, two = 2, three = 3;
return { one: one, three: three, two: two };
})(), one = _c.one, two = _c.two, three = _c.three;
function sayHello(age, name='dude'){
console.log(`Hello ${name}`);
}
sayHello(12);
function sayHello(age, name) {
if (name === void 0) { name = 'dude'; }
console.log("Hello " + name);
}
sayHello(12);
function add(...x) {
return x.reduce((m, n) => m + n);
}
function add() {
var x = [];
for (var _i = 0; _i < arguments.length; _i++) {
x[_i - 0] = arguments[_i];
}
return x.reduce(function (m, n) { return m + n; });
}
{
let a = 10;
}
a; // IDE或编译器会告诉你:cannot find name 'a'
const c = 123;
c = 321; // IDE或编译器会告诉你:Left-hand side of assignment expression cannot be a constant.
const items = [1,2,3];
const itemsCopy = [...items];
var items = [1, 2, 3];
var itemsCopy = items.slice();
为什么要将类型加到 Javascript中呢?
但这并不意味着我们需要重复写各种类型声明,现代编程语言提供一种叫类型推断(type inference)
的方式来简化类型的声明,例如:
var foo = 123;
foo = '456'; // Error: cannot assign `string` to `number`
帅呆了,编译器通过上下文,知道 foo 变量应当是一个数字,所以当想将其设置成字符串的时候,编译器报错。
为了让 Javascript开发简洁,类型并非是强制的,而是只要结构上一致便可以使用,例如:
interface Point2D {
x: number;
y: number;
}
interface Point3D {
x: number;
y: number;
z: number;
}
var point2D: Point2D = { x: 0, y: 10, }
var point3D: Point3D = { x: 0, y: 10, z: 20 }
function iTakePoint2D(point: Point2D) { /* do something */ }
iTakePoint2D(point2D); // 精准匹配
iTakePoint2D(point3D); // 有额外的信息,没有问题
iTakePoint2D({ x: 0 }); // Error: missing information `y`
ES7 将引入奇怪的 Typed Objects,不过目前看来 ES7 还是遥遥无期,如果想拥有类型,还是先使用 TypeScript吧。