具體函數細節:
// target:目標函數 // this:綁定的上下文對象 // arguments:函數的參數列表 Reflect.apply(target, this, arguments) const arr = [2, 3, 4, 5, 6]; let max; // ES6 max = Reflect.apply(Math.max, null, arr) // ES5 max = Math.max.apply(null, arr); max = Function.prototype.apply.call(Math.max, null, arr);
// 這個方法,提供了一種新的不使用new來調用構造函數的方法 function A(name) { console.log('Function A is invoked!'); this.name = name; } A.prototype.getName = function() { return this.name; }; function B(age) { console.log('Function B is invoked!'); this.age = age; } B.prototype.getAge = function() { return this.age; }; // 測試 (這兩種是一致的) var tom = new A('tom'); var tom = Reflect.construct(A, ['tom']); // jnney繼承了A的實例屬性,同時繼承了B的共享屬性 // 簡單來說,A構造函數被調用,但是 jnney.__proto__ === B.prototype var jnney = Reflect.construct(A, ['jnney'], B);
這個方法和Object.definePropperty
(屬性定義失敗,會拋出一個錯誤,成功則返回該對象)相似,不過Reflect.defineProperty
(屬性定義失敗,返回false,成功則返回true)返回的是一個Boolean值。
let obj = {}; let obj1 = Object.defineProperty(obj, 'name', { enumerable: true, value: 'bjw' }); // 這里會返回false 因為我們上面定義name這個屬性是不可修改的, // 然后我們又在這里修改了name屬性,所以修改失敗返回值為false let result1 = Reflect.defineProperty(obj, 'name', { configurable: true, enumerable: true, value: 'happy' }); console.log(result1); // false
let obj = { name: 'dreamapple', age: 22 }; let r1 = Reflect.deleteProperty(obj, 'name'); console.log(r1); // true let r2 = Reflect.deleteProperty(obj, 'name'); console.log(r2); // true let r3 = Reflect.deleteProperty(Object.freeze(obj), 'age'); console.log(r3); // false
Reflect.set(target, propertyKey, value[, receiver])
這個方法用來讀取/設置一個對象的屬性,target是目標對象,propertyKey
是我們要讀取的屬性,receiver
是可選的,如果propertyKey
的getter
函數里面有this
值,那么receiver
就是這個this
所代表的上下文。
Reflect.getOwnPropertyDescriptor(target, propertyKey)
這個方法與Object.getOwnPropertyDescriptor
方法類似,其中target
是目標對象,propertyKey
是對象的屬性,如果這個屬性存在屬性描述符的話就返回這個屬性描述符;如果不存在的話,就返回undefined
。(如果第一個參數不是對象的話,那么Object.getOwnPropertyDescriptor
會將這個參數強制轉換為對象,而方法 Reflect.getOwnPropertyDescriptor
會拋出一個錯誤。)
var obj = {age: 22} Reflect.getOwnPropertyDescriptor(obj, 'age') {value: 22, writable: true, enumerable: true, configurable: true}
Reflect.setPrototypeOf(target, prototype)
這個方法與Object.getPrototypeOf
方法是一樣的,都是返回一個對象的原型,也就是內部的[[Prototype]]屬性的值。
Reflect.setPrototypeOf
與Object.setPrototypeOf
方法的作用是相似的,設置一個對象的原型,如果設置成功的話,這個對象會返回一個true;如果設置失敗,這個對象會返回一個false。
這個方法相當于ES5的in操作符,就是檢查一個對象上是否含有特定的屬性;我們繼續來實踐這個方法:
function A(name) { this.name = name || 'dreamapple'; } A.prototype.getName = function() { return this.name; }; var a = new A(); console.log('name' in a); // true console.log('getName' in a); // true let r1 = Reflect.has(a, 'name'); let r2 = Reflect.has(a, 'getName'); console.log(r1, r2); // true true
這個函數檢查一個對象是否是可以擴展的,也就是是否可以添加新的屬性。(要求target必須為一個對象,否則會拋出錯誤)
let obj = {}; let r1 = Reflect.isExtensible(obj); console.log(r1); // true // 密封這個對象 Object.seal(obj); let r2 = Reflect.isExtensible(obj); console.log(r2); // false
使用模塊化,可以為我們帶來以下好處:
在早期,使用立即執行函數實現模塊化,通過函數作用域解決了命名沖突、污染全局作用域的問題。
這兩種實現方式已經很少見到,具體的使用方式如下:
// AMD define(['./a', './b'],function(a, b){ // 模塊加載完畢可以使用 a.do(); b.do(); }); // CMD define(function(require, exports, module){ // 加載模塊 var a = require('./a'); });
CommonJS最早是Node在使用,目前可以在Webpack中見到它。
// a.js module.exports = { a: 1 } // or exports.a = 1; // 在b.js中可以引入 var module = require('./a'); module.a // log 1
難點解析:
// module 基本實現 var module = { id: 'xxx', exports: {} } var exports = module.exports; // 所以,通過對exports重新賦值,不能導出變量
ES Module 是原生實現模塊化方案。
// 導入模塊 import xxx form './a.js'; import { xxx } from './b.js'; // 導出模塊 export function a(){} // 默認導出 export default {}; export default function(){}
const PENDING = 'pending'; const RESOLVED = 'resolved'; const REJECTED = 'rejected'; function MyPromise(fn) { const _this = this; _this.state = PENDING; _this.value = null; _this.resolvedCallbacks = []; _this.rejectedCallbacks = []; // resolve函數 function resolve(value) { if (_this.state === PENDING) { _this.state = RESOLVED; _this.value = value; _this.resolvedCallbacks.map(cb => cb(_this.value)); } } // rejected函數 function reject(value) { if (_this.state === PENDING) { _this.state = REJECTED; _this.value = value; _this.rejectedCallbacks.map(cb => cb(_this.value)); } } // 當創建對象的時候,執行傳進來的執行器函數 // 并且傳遞resolve和reject函數 try { fn(resolve, reject); } catch (e) { reject(e); } } // 為Promise原型鏈上添加then函數 MyPromise.prototype.then = function (onFulfilled, onRejected) { const _this = this; onFulfilled = typeof onFulfilled === 'function' ? onFulfilled : v => v; onRejected = typeof onRejected === 'function' ? onRejected : r => { throw r; } if (_this.state === PENDING) { _this.resolvedCallbacks.push(onFulfilled); _this.rejectedCallbacks.push(onRejected); } if (_this.state === RESOLVED) { onFulfilled(_this.value); } if (_this.state === REJECTED) { onRejected(_this.value); } return _this; } // 測試 new MyPromise(function (resolve, reject) { setTimeout(() => { resolve('hello'); }, 2000); }).then(v => { console.log(v); }).then(v => { console.log(v + "1"); })
這篇文章就介紹到這了,需要的朋友可以參考一下。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com