自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從 Vue3 源碼學(xué)習(xí) Proxy & Reflect

開(kāi)發(fā) 前端
如果你使用的是Vue框架,嘗試修改組件的 props 對(duì)象,它將觸發(fā)Vue的警告日志,這個(gè)功能是使用 Proxy :) !

[[436812]]

這兩個(gè)功能都出現(xiàn)在ES6中,兩者配合得非常好!

Proxy

**proxy **是一個(gè)外來(lái)的對(duì)象,他沒(méi)有屬性! 它封裝了一個(gè)對(duì)象的行為。它需要兩個(gè)參數(shù)。

  1. const toto = new Proxy(target, handler) 

**target: **是指將被代理/包裹的對(duì)象 **handler: **是代理的配置,它將攔截對(duì)目標(biāo)的操作(獲取、設(shè)置等)

多虧了 proxy ,我們可以創(chuàng)建這樣的 traps :

  1. const toto = { a: 55, b:66 } 
  2. const handler = { 
  3.  get(target, prop, receiver) { 
  4.    if (!!target[prop]) { 
  5.      return target[prop] 
  6.     } 
  7.     return `This ${prop} prop don't exist on this object !` 
  8.   } 
  9.  
  10. const totoProxy = new Proxy (toto, handler) 
  11.  
  12. totoProxy.a // 55 
  13. totoProxy.c // This c prop don't exist on this object ! 

 每個(gè)內(nèi)部對(duì)象的 "方法" 都有他自己的目標(biāo)函數(shù)

下面是一個(gè)對(duì)象方法的列表,相當(dāng)于進(jìn)入了 Target:

  1. object methodtargetobject[prop]getobject[prop] = 55setnew Object()constructObject.keysownKeys 

目標(biāo)函數(shù)的參數(shù)可以根據(jù)不同的函數(shù)而改變。

例如,對(duì)于get函數(shù)取(target, prop, receiver(proxy本身)),而對(duì)于 set 函數(shù)取(target, prop, value (to set), receiver)

例子

我們可以創(chuàng)建私有屬性。

  1. const toto = { 
  2.    name'toto'
  3.    age: 25, 
  4.    _secret: '***' 
  5.  
  6. const handler = { 
  7.   get(target, prop) { 
  8.    if (prop.startsWith('_')) { 
  9.        throw new Error('Access denied'
  10.     } 
  11.     return target[prop] 
  12.   }, 
  13.   set(target, prop, value) { 
  14.    if (prop.startsWith('_')) { 
  15.        throw new Error('Access denied'
  16.     } 
  17.     target[prop] = value 
  18.     // set方法返回布爾值 
  19.     // 以便讓我們知道該值是否已被正確設(shè)置 ! 
  20.     return true 
  21.   }, 
  22.   ownKeys(target, prop) { 
  23.      return Object.keys(target).filter(key => !key.startsWith('_')) 
  24.   }, 
  25.  
  26. const totoProxy = new Proxy (toto, handler) 
  27. for (const key of Object.keys(proxy1)) { 
  28.   console.log(key) // 'name''age' 

Reflect

Reflect 是一個(gè)靜態(tài)類,簡(jiǎn)化了 proxy 的創(chuàng)建。

每個(gè)內(nèi)部對(duì)象方法都有他自己的 Reflect 方法

  1. object methodReflectobject[prop]Reflect.getobject[prop] = 55Reflect.setobject[prop]Reflect.getObject.keysReflect.ownKeys 

❓ 為什么要使用它?因?yàn)樗蚉roxy一起工作非常好! 它接受與 proxy 的相同的參數(shù)!

  1. const toto = { a: 55, b:66 } 
  2.  
  3. const handler = { 
  4.   get(target, prop, receiver) { 
  5.    // 等價(jià) target[prop] 
  6.    const value = Reflect.get(target, prop, receiver) 
  7.    if (!!value) { 
  8.       return value  
  9.    } 
  10.    return `This ${prop} prop don't exist on this object !`  
  11.   }, 
  12.   set(target, prop, value, receiver) { 
  13.      // 等價(jià)  target[prop] = value 
  14.      // Reflect.set 返回 boolean 
  15.      return Reflect.set(target, prop, receiver) 
  16.   }, 
  17.  
  18. const totoProxy = new Proxy (toto, handler) 

 所以你可以看到 Proxy 和 Reflect api是很有用的,但我們不會(huì)每天都使用它,為了制作陷阱或隱藏某些對(duì)象的屬性,使用它可能會(huì)很好。

如果你使用的是Vue框架,嘗試修改組件的 props 對(duì)象,它將觸發(fā)Vue的警告日志,這個(gè)功能是使用 Proxy :) !

作者:CodeOz 譯者:前端小智

來(lái)源:dev 原文:https://dev.to/codeoz/proxy-reflect-api-in-javascript-51la

 

責(zé)任編輯:姜華 來(lái)源: 今日頭條
相關(guān)推薦

2021-12-01 08:11:44

Vue3 插件Vue應(yīng)用

2021-11-30 08:19:43

Vue3 插件Vue應(yīng)用

2023-11-28 09:03:59

Vue.jsJavaScript

2021-09-22 07:57:23

Vue3 插件Vue應(yīng)用

2022-04-14 09:35:03

Vue.js設(shè)計(jì)Reflect

2021-11-16 08:50:29

Vue3 插件Vue應(yīng)用

2021-12-02 05:50:35

Vue3 插件Vue應(yīng)用

2025-02-25 08:51:19

2020-09-17 07:08:04

TypescriptVue3前端

2022-01-26 11:00:58

源碼層面Vue3

2021-12-08 09:09:33

Vue 3 Computed Vue2

2021-12-29 07:51:21

Vue3 插件Vue應(yīng)用

2021-09-27 06:29:47

Vue3 響應(yīng)式原理Vue應(yīng)用

2020-09-19 21:15:26

Composition

2021-07-29 12:05:18

Vue3Api前端

2023-12-11 07:34:37

Computed計(jì)算屬性Vue3

2024-07-01 13:45:18

2021-03-22 10:05:25

開(kāi)源技術(shù) 項(xiàng)目

2021-11-17 08:24:47

Vue3 插件Vue應(yīng)用

2023-12-14 08:25:14

WatchVue.js監(jiān)聽(tīng)數(shù)據(jù)
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)