<script type="text/javascript"> 'use strict'; // 使用嚴格模式 var str = 'string'; // 必須使用var來聲明變量 function Person(name, age) { this.name = name; this.age = age; } // Person(); // 禁止自定義的函數中的this指向window new Person('zh', 18); var str = '123'; eval('var str = "234"; alert(str)'); // 建立eval做用域不會污染全局變量 alert(str); // 對象不能有重名的屬性 var obj = { name: '123', name: '234' } <script
var obj = { uername: 'zh', age: 18 }; var obj1 = Object.create(obj, { sex: { value: '女', writable: true, configurable: true, enumerable: true } })Object.defineProperties(object, descriptors)
var obj2 = { firstName: '123', lastName: '234' } Object.defineProperties(obj2, { fullName: { get: function () { // 惰性求值,獲取擴展屬性的值,獲取擴展屬性值的get方法自動調用 return this.firstName + ' '+ this.lastName; }, set: function (data) { // 監聽擴展屬性,當擴展屬性發生變化的時候會自動調用,會將變化的值做爲實參注入到set函數 console.log('Set()---' + data); var names = data.split(' '); this.firstName = names[0]; this.lastName = names[1]; } } }) obj2.firstName = '456' console.log(obj2.fullName); // 456 234 console.log(obj2); // {firstName: "456", lastName: "234"} obj2.fullName = 'change fullName' console.log(obj2.fullName); // change fullName
var obj = { firstName: '111', lastName: '222', get fullName() { return this.firstName + ' ' + this.lastName; }, set fullName(data) { var names = data.split(' '); this.firstName = names[0]; this.lastName = names[1]; } }; console.log(obj); obj.fullName = '333 444'; console.log(obj);
var arr = [1, 2, 3, 4, 5, 6, 7, 8]; var newArr = arr.map(function (item, index) { return item + 10; }) console.log(newArr); // [11, 12, 13, 14, 15, 16, 17, 18]
var arr = [1, 2, 3, 4, 5, 6, 7, 8]; var newArr1 = arr.filter((item, index) => item > 3 ) console.log(newArr1); // [4, 5, 6, 7, 8]
{ // console.log(sum); // 不會預處理(預解析),不存在變量提高 let sum = 123; // let sum = 123; // 不能重複聲明 } // console.log(sum); // 在塊級做用域內有效,在ES5及以前沒有塊級做用域
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>01_let關鍵字</title> </head> <body> <button>測試1</button> <br> <button>測試2</button> <br> <button>測試3</button> <br> <script type="text/javascript"> let btns = document.getElementsByTagName('button'); // 閉包 // for (let i = 0; i < btns.length; i++) { // (function (i) { // btns[i].onclick = function () { // alert(i); // } // })(i) // } // 可代替閉包 for (let i = 0; i < btns.length; i++) { btns[i].onclick = function () { alert(i); } } </script> </body> </html>
// 解構數組 let arr = [1, 3, 5, 'abc', true]; let [, , a, b] = arr // 5 "abc"
// 對象的解構賦值 let { username, age, xxx } = obj; // 至關於 // let username = obj.username; // 因爲let不能重複聲明變量,故會在控制檯中報錯 // let age = obj.age; // let xxx = obj.xxx;
let obj = { userName: 'zh', age: 18 }; let str = '個人名字叫:' + obj.userName + ',個人年齡是' + obj.age; let tempalteStr = `個人名字叫:${obj.userName},個人年齡是${obj.age}`;
let username = 'zh'; let age = 18; let obj = { // 同名屬性可省略不寫 username, age, getName() { // 能夠省略函數的function return this.username; } };
let obj2 = { name: '123', age: 18, text: () => { btn3.onclick = () => { console.log(this); // window console.log(this === window); // true } } } obj2.text();
function Point(x = 1 , y = 1) { this.x = x; this.y = y; }
function foo(a, ...value) { console.log(arguments); // 僞數組 Arguments(4) [1, 2, 3, 4, callee: (...)] console.log(value); // 數組 [2, 3, 4] } foo(1, 2, 3, 4);
var arr = [1,6] var arr1 = [2,3,4,5]; arr = [1,...arr1,6]; console.log(arr); // [1, 2, 3, 4, 5, 6] console.log(...arr); // 1 2 3 4 5 6
// 定義一我的物的類 class Person { constructor (name,age) { // 表示類的構造方法 this.name = name; this.age = age; } // 類的通常方法 showName() { console.log(this.name); } } let person = new Person ('000', 18); console.log(person); person.showName(); // 子類 class Child extends Person { constructor (name,age,salary) { super(name, age); // 調用父類的構造方法 this.salary = salary; } showName () { console.log(this.name, this.age, this.salary) } } let child = new Child('www', 18, 10000 ); console.log(child); child.showName();
let promise = new Promise((resolve, reject) => { //初始化promise狀態爲 pending //執行異步操做 if(異步操做成功) { resolve(value);//修改promise的狀態爲fullfilled } else { reject(errMsg);//修改promise的狀態爲rejected } })
promise.then(function( result => console.log(result), errorMsg => alert(errorMsg) ))
// 建立promiise對象 let promise = new Promise((resolve, reject) => { // 初始化promise狀態:pending console.log(111); // 執行異步操做,一般是發送ajax請求,開啓定時器 setTimeout(() => { console.log(333); // 根據異步任務的返回結果取修改promise的狀態 // 異步任務執行成功 // resolve('成功標記'); // 修改promise的狀態爲 fullfilled:成功的狀態 // 異步任務執行失敗 reject('失敗標記');// 修改promise的狀態爲 rejected:失敗的狀態 }, 2000) }) console.log(222); promise.then((data) => { // 成功的回調 console.log(data,'成功了') }, (error) => { // 失敗的回調 console.log(error,'失敗了')
let symbol = Symbol(); let obj = {}; obj[symbol] = 'hello';
let symbol = Symbol('one'); let symbol2 = Symbol('two'); console.log(symbol);// Symbol('one') console.log(symbol2);// Symbol('two')
// 建立symbol屬性值 let symbol = Symbol(); console.log(symbol); let obj = {username: 'zh', age: 18}; obj[symbol] = 'symbol'; // 不能使用obj.symbol = '111' console.log(obj[symbol]); // for in, for of 不能遍歷symbol屬性 for (let i in obj) { console.log(i); } // 每建立一個symbol的值都須要經過 let symbol2 = Symbol('one'); let symbol3 = Symbol('two'); console.log(symbol2 == symbol3); // false console.log(symbol2, symbol3); // Symbol(one) Symbol(two) // 能夠去定義常量 const Person_key = Symbol('person_key'); console.log(Person_key)
let targetData = { [Symbol.iterator]: function () { let nextIndex = 0; // 記錄指針的位置 return { // 遍歷器對象 next: () => { return nextIndex < this.length ? { value: this[nextIndex++], done: false } : { value: undefined, done: true }; } } } }
function* generatorExample(){ let result = yield 'hello'; // 狀態值爲hello yield 'generator'; // 狀態值爲generator }
// generator函數 function* myGenerator() { console.log('第一次執行'); let result = yield 'hello'; // 當調用第一次 MG.next() 時,指針停在此處 console.log('第二次執行'); console.log(result); // aaaaa yield 'generator' console.log('遍歷完成') return "完成" } let MG = myGenerator(); // 返回一個遍歷器對象/遍歷器對象(Iterator) console.log(MG.next()); //done屬性反映當前generator是否遍歷完成,false表示沒有遍歷完 console.log(MG.next('aaaaa')); // next()調用的時候傳參,傳入的參數會做爲next方法啓動時候的返回值 console.log(MG.next()); //
async function foo(){ await 異步操做; await 異步操做; }
let arr2 = [1,2,3,4,5,6,7,8]; var result = arr2.find((item, index) => { return item > 4; }) console.log(result) // 5
let obj = {}; let obj1 = {username:"anverson", age: 42}; let obj2 = {sex: "女"}; Object.assign(obj, obj1); // 將源對象的屬性複製到目標對象上 Object.assign(obj, obj2); console.log(obj); // {username: "anverson", age: 42, sex: "女"}
let obj2 = {};
obj2.__proto__ = obj1;
// 檢測數據類型函數 function checkedType(target) { return Object.prototype.toString.call(target).slice(8, -1); } // 實現深度克隆 ---> 對象/數組 function clone(target) { let result, targetType = checkedType(target); if (targetType === 'Object') { result = {}; } else if (targetType === 'Array') { result = [] } else { return target; } // 遍歷目標數據 for (i in target) { let value = target[i]; // 判斷目標結構裏的每一項值是否存在對象/數組 if (checkedType(value) === 'Object' || checkedType(value) === 'Array') { // 繼續遍歷獲取到的value result[i] = clone(value); } else { result[i] = value; } } return result; }
// set let set = new Set([1, 3, 5, 5, 4, 8]); // 會將重複的5刪除 console.log(set.size, set); set.add(7); console.log(set.size, set); console.log(set.has(8)); console.log(set.has(0)); set.clear(); console.log(set.size, set); // map let map = new Map([['aaa', 'username'], ['ccc', 'username']]); console.log(map.size, map); map.set('age', 111); console.log(map.size, map); map.delete('aaa'); console.log(map.size, map);
// 利用set給數組去重 function removeSame(arr) { let newArr = []; let set = new Set(arr); for (i of set) { newArr.push(i) } return newArr; }
console.log(3**3); // 3的3次冪 --- 9
let arr = [1,2,6,'abc']; console.log(arr.includes('a')); // false