阅读:7722 次 编辑日期:2016-12-26
//ES5 var name = "uw3c"; if(true){ var name="javascript"; } console.log(name) //javascript
//ES6 let name = "uw3c"; if(true){ let name="javascript"; } console.log(name) //uw3c
//ES5 var a = []; for (var i = 0; i < 10; i++) { a[i] = function () { alert(i); }; } a[6](); // 10
//ES5 var a = []; for (var i = 0; i < 10; i++) { a[i] = (function (index) { return function(){alert(index)}; })(i); } a[6](); // 6
//ES6 var a = []; for (let i = 0; i < 10; i++) { a[i] = function () { console.log(i); }; } a[6](); // 6
//ES6 const PI = Math.PI; PI = 99; console.log(PI);//Assignment to constant variable
//ES5 function Animal(){ this.type= "animal"; } Animal.prototype.say = function(sound){ alert(this.type+ ",say:" + sound); } var An = new Animal(); An.say("hello");//animal,say:hello function Cat(){}; Cat.prototype = new Animal(); Cat.prototype.type = "cat"; var blackCat = new Cat(); blackCat.say("hi");//cat,say:hi
//ES6 class Animal { constructor(){ this.type = "animal"; } says(sound){ alert(this.type + ",says:" + sound) } } let animal = new Animal() animal.says("hello") //animal,says: hello class Cat extends Animal { constructor(){ super(); this.type = "cat"; } } let cat = new Cat(); cat.says("hi") //cat,says:hi
//ES5 function(i){ return i + 1; } //ES6 (i) => i + 1
//ES5 function auto(name){ name = name || "uw3c" console.log(name); } auto(); //ES6 function auto(name = "uw3c"){ console.log(name); auto(); }
//ES5 function Fun1(){ var str = Fun1.arguments[0]; alert(str);//uw3c } Fun1("uw3c") //ES6 function Fun2(...types){ var str = types[0]; alert(str);//uw3c } Fun2("uw3c")
//a.js var name="uw3c"; var echo=function(value){ console.log(value) } export {name,echo} // b.js import {name,echo} from "./a.js" console.log(sex) // uw3c echo(sex) // uw3c