基礎變化前端
//String changes var a = "Hello world"; var b = "Hello"; var c = "world"; function includes(source, dest) { return source.indexOf(dest) > -1; } function startsWith(source, dest) { return source.slice(0, dest.length) === dest; } function endsWith(source, dest) { return source.slice(source.length - dest.length, source.length) === dest; } var msg = "Hello world!"; console.log("msg startsWith Hello: ", msg.startsWith("Hello")); // true console.log("msg endsWith !: ", msg.endsWith("!")); // true console.log("msg includes o: ", msg.includes("o")); // true console.log("msg startsWith o: ", msg.startsWith("o")); // false console.log("msg endsWith world!: ", msg.endsWith("world!")); // true console.log("msg includes x: ", msg.includes("x")); // false
console.log(+0 == -0); // true console.log(+0 === -0); // true console.log(Object.is(+0, -0)); // false console.log(NaN == NaN); // false console.log(NaN === NaN); // false console.log(Object.is(NaN, NaN)); // true console.log(5 == 5); // true console.log(5 == "5"); // true console.log(5 === 5); // true console.log(5 === "5"); // false console.log(Object.is(5, 5)); // true console.log(Object.is(5, "5")); // false
function getValue(condition) { if (condition) { let value = "blue"; // other code return value; } else { // value doesn't exist here return null; } // value doesn't exist here }
var options = { repeat: true, save: false, rules: { custom: 10, } }; // later var { repeat, save, rules: { custom }} = options; console.log(repeat); // true console.log(save); // false console.log(custom); // 10
類es6
//class declaration function PersonType(name) { this.name = name; } PersonType.prototype.sayName = function() { console.log(this.name); }; let person = new PersonType("Nicholas"); person.sayName(); // outputs "Nicholas" console.log(person instanceof PersonType); // true console.log(person instanceof Object); // true (function(){ 'use strict'; class PersonClass { constructor(name) { this.name = name; } sayName() { console.log(this.name); } } let person = new PersonClass("Nicholas"); person.sayName(); // outputs "Nicholas" console.log(person instanceof PersonClass); console.log(person instanceof Object); })()
//Accessor Properties (function(){ 'use strict'; class PersonClass { constructor(name) { this.name = name; } get Name(){ return this.name; } set Name(value){ this.name = value; } } let person = new PersonClass("Nicholas"); console.log('person.Name: ', person.Name) // outputs "Nicholas" })()
//ES5 function PersonType(name) { this.name = name; } // static method PersonType.create = function(name) { return new PersonType(name); }; // instance method PersonType.prototype.sayName = function() { console.log(this.name); }; var person = PersonType.create("Nicholas"); //ES6 //Static Members (function(){ 'use strict'; class PersonClass { constructor(name) { this.name = name; } sayName() { console.log(this.name); } static create(name) { return new PersonClass(name); } } let person = PersonClass.create("Nicholas"); console.log(person); })()
//Handling Inheritance (function(){ 'use strict'; class PersonClass { constructor(name) { this.name = name; } } class Developer extends PersonClass { constructor(name, lang) { super(name); this.language = lang; } } var developer = new Developer('coder', 'Javascript'); console.log("developer.name: ", developer.name); console.log("developer.language: ", developer.language); })()
模塊機制json
當前關於JS的模塊化已有兩個重要的規範CommonJs和AMD,但畢竟不是原生的模塊化,因此ES6中引入模塊化機制,使用export和import來聲明暴露的變量和引入須要使用的變量promise
Iterator和Generator前端框架
Iterator擁有一個next方法,該方法返回一個對象,該對象擁有value屬性表明這次next函數的值、done屬性表示是否擁有繼續擁有可返回的值;done爲true時表明沒有多餘的值能夠返回此時value爲undefined;Generator函數使用特殊的聲明方式,generator函數返回一個iterator對象,在generator函數內部的yield關鍵字聲明瞭next方法的值app
//Iterator & Generator // generator function *createIterator() { yield 1; yield 2; yield 3; } // generators are called like regular functions but return an iterator var iterator = createIterator(); console.log(iterator.next()); console.log(iterator.next()); console.log(iterator.next()); console.log(iterator.next());
Promise框架
ES6引入原生的Promise對象,Promise構造函數接受一個方法做爲參數,該方法中能夠調用resolve和reject方法,分別進入fulfill狀態和fail狀態ecmascript
// Promise var getJSON = function(url) { var promise = new Promise(function(resolve, reject){ var client = new XMLHttpRequest(); client.open("GET", url); client.onreadystatechange = handler; client.send(); function handler() { if (this.readyState !== 4) { return; } if (this.status === 200) { debugger; resolve(this.responseText); } else { reject(new Error(this.statusText)); } }; }); return promise; }; getJSON("https://gis.lmi.is/arcgis/rest/services/GP_service/geocode_thjonusta_single/GeocodeServer?f=json").then(function(json) { console.log('Contents: ' + json); }, function(error) { console.error('Error: ', error); });
Proxy模塊化
顧名思義用來做爲一個對象或函數的代理。Proxy構造函數接受兩個參數:target用來被封裝的對象或函數、handler擁有一系列方法,重寫這些方法以便當調用這些操做時會進入重寫的方法中函數
handler.getPrototypeOf
handler.setPrototypeOf
handler.isExtensible
handler.preventExtensions
handler.getOwnPropertyDescriptor
handler.defineProperty
handler.has
handler.get
handler.set
handler.deleteProperty
handler.enumerate
handler.ownKeys
handler.apply
handler.construct
參考資料: