/**
* 字符串擴展
*/
// let xukai = 'aaaaxxxxx'
//字符串增長方法 includes() startsWith() endsWith()
// let str = 'xukai'.repeat(3) //循環次數
// let str = 'xukai'.padStart(8,'aa') padEnd //補全功能
// let str = `
// aaa${xukai}aaasdsd
// `
// console.log(str)
/**
* 函數擴展
*/
// function log (x , y = 'xukai') { //能夠加默認值 命名不能相同
// console.log(x, y)
// }
// log ('')
// var f = v => v
// var f = function (v) {
// return v
// }
// var sum = ( a , b ) => a + b
// let func = id => ({id:id , name : 'xukai'}) //返回值爲對象須要用()
// [1,2,3].map(function () {
// return x*x
// })
// [1,2,3].map(() => x*x)
// let res = [12323,2123].sort((a,b) => a-b)
// 箭頭函數有幾個使用注意點。
// (1)函數體內的this對象,就是定義時所在的對象,而不是使用時所在的對象。
// (2)不能夠看成構造函數,也就是說,不可使用new命令,不然會拋出一個錯誤。
// (3)不可使用arguments對象,該對象在函數體內不存在。若是要用,能夠用 rest 參數代替。
// (4)不可使用yield命令,所以箭頭函數不能用做 Generator 函數。
// 上面四點中,第一點尤爲值得注意。this對象的指向是可變的,可是在箭頭函數中,它是固定的。
// this指向的固定化,並非由於箭頭函數內部有綁定this的機制,實際緣由是箭頭函數根本沒有本身的this,致使內部的this就是外層代碼塊的this。正是由於它沒有this,因此也就不能用做構造函數。
// ES6
// function foo() {
// setTimeout(() => {
// console.log('id:', this.id);
// }, 100);
// }
// ES5
// function foo() {
// var _this = this;
// setTimeout(function () {
// console.log('id:', _this.id);
// }, 100);
// }
/**
* 數組擴展
*/
// const numn = [1,2,3,43,5,54,34]
// let add = (a,b,c) => a + b + c
// console.log(add(...numn))
// let xx = Array.of('122',21,'xuakl') //將一組數值轉換爲數組
// console.log(xx)
// Array.of方法能夠用下面的代碼模擬實現。
// function ArrayOf(){
// return [].slice.call(arguments);
// }
// [1,2,34,21,4,325,1].find((n) => n <22) //find查找
// new Array(3).fill(5) //填充值 fill(填充值,起始位,結束位)
// [1,2,3,4].includes(2) //true 包含
// if (arr.indexOf(el) !== -1) {
// // ...
// }
/**
* 對象擴展
*/
// const o = {
// method() {
// return "Hello!";
// }
// };
// 等同於
// const o = {
// method: function() {
// return "Hello!";
// }
// };
// this關鍵字老是指向函數所在的當前對象,ES6 又新增了另外一個相似的關鍵字super,指向當前對象的原型對象。super關鍵字表示原型對象時,只能用在對象的方法之中,用在其餘地方都會報錯。
// 對象的擴展運算符等同於使用Object.assign()方法。
// let aClone = { ...a };
// // 等同於
// let aClone = Object.assign({}, a);
// Object.is('foo','foo') //比較
// const target = { a: 1 };
// const source1 = { b: 2 };
// const source2 = { c: 3 };
// Object.assign(target, source1, source2);
// target // {a:1, b:2, c:3}
// Object.assign方法的第一個參數是目標對象,後面的參數都是源對象。
// 注意,若是目標對象與源對象有同名屬性,或多個源對象有同名屬性,則後面的屬性會覆蓋前面的屬性。
/**
* Set和Map數據結構
*/
// Set 是一個沒有重複值的數組
// [... new Set([1,2,1,2,3,4,4])]
// Set.prototype.constructor:構造函數,默認就是Set函數。
// Set.prototype.size:返回Set實例的成員總數。
// add(value):添加某個值,返回 Set 結構自己。
// delete(value):刪除某個值,返回一個布爾值,表示刪除是否成功。
// has(value):返回一個布爾值,表示該值是否爲Set的成員。
// clear():清除全部成員,沒有返回值。
// Array.from方法能夠將 Set 結構轉爲數組。
// function dedupe(array) { //數組去重
// return Array.from(new Set(array));
// }
// dedupe([1, 1, 2, 3]) // [1, 2, 3]
// var arr = [1, 2, 3]
// var iterator = arr[Symbol.iterator]()
// console.log(iterator.next())
// console.log(iterator.next())
// console.log(iterator.next())
// console.log(iterator.next())
複製代碼