Home > Web Front-end > JS Tutorial > Detailed introduction to proxies in es6 (code example)

Detailed introduction to proxies in es6 (code example)

不言
Release: 2018-11-17 16:17:16
forward
1925 people have browsed it

This article brings you a detailed introduction (code example) about the agent in es6. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.

Overview

Agency means asking an agent to do something on your behalf, but unlike yourself, the agent can have his own behavior, and can even do things that go against expectations. the behavior of.

Chestnut

Declare a common object origin, which has an attribute name

let origin={
    name: 'origin'
}
Copy after login

Declare a proxy object

let proxy=new Proxy(origin, {
    get: (target, key)=>target[key]+" from proxy",
    set: (target, key, value)=>target[key]="set by proxy "+value
})
Copy after login

Output at this timeorigin and proxy, you can find that proxy has the same name attribute as origin

console.log(origin) // {name: 'origin'}
console.log(proxy) // Proxy {name: 'origin'}
Copy after login

is originAdd the age attribute, and then output it. You can find that both origin and proxy have the age attribute

origin.age=1 
console.log(origin) // {name: 'origin', age: '1'}
console.log(proxy) // Proxy {name: 'origin', age '1'}
Copy after login

Is that a proxy? Of course not, we try to add attributes to proxy

proxy.x=1
console.log(origin) // {name: 'origin', age: '1', x:'set by proxy 1'}
console.log(proxy) // Proxy {name: 'origin', age '1'}
Copy after login

and we can find that although origin and proxy They all have the x attribute, but it is not the 1 we assigned, but a few more strings set by proxy . Obviously, this is executed The set method of the second object passed in when initializing proxy
So what if we get

console.log(proxy.x) // set by proxy 1
console.log(proxy.x) // set by proxy 1 from proxy
Copy after login

is now clear , proxy is the proxy of origin, all operations on proxy will be synchronized to origin, and for origin# The operations of ## will not be synchronized to proxy, and proxy also has its own behavior.

You can think of it this way,

proxy is the secretary of origin. All transactions must be submitted to the secretary. The secretary has his own rules for doing things and can submit them directly to the boss. , you can also reject the submission, or add some other behaviors before submitting. So what can this secretary do on behalf of the boss?

Trap

Syntax

let p = new Proxy(target, handler);
Copy after login
Initializing a proxy requires two parameters

target: proxy target

handle: A trap is an object. Our operation is like an escaping animal. If the hunter places all the traps on all the ways to escape, then we will always fall into one together. . The essence is an object, the key describes our operation, the value is the function, describes our behavior, there are a total of 13 traps.

0x003

set:Set attributes

Syntax:

set(target, key, value)
Copy after login

target: Proxy object

key: Set attribute

value: Set attribute value

Chestnut:

let origin={}
let proxy=new Proxy(origin,{
    set:(target, key, value)=>{
        if(value>5)target[key]=value+10000
    }
})
proxy.x=1
proxy.y=10
console.log(proxy) // Proxy {y: 10010}
console.log(origin) // {y: 10010}
Copy after login
Explanation:

We placed a
set above Trap, when we do the set operation, it will be caught. We judge whether value is greater than 5, if not greater than 5We will not do anything, but if it is greater than 5, we will perform an assignment operation and add 10000. The chestnut above is equivalent to an interceptor.

get: access attribute

Syntax:

get(target, key)
Copy after login

target: proxy object

key: access Properties

Chestnuts:

let origin={
    x:1,
    y:2
}
let proxy=new Proxy(origin,{
    get:(target, key)=>{
        if(key==='x')return 'no'
        return target[key]
    }
})
console.log(proxy.x) // 'no'
console.log(proxy.y) // 2
Copy after login

deleteProperty: Delete properties

Syntax:

deleteProperty(target, key)
Copy after login

target: Proxy object

key: Attribute to be deleted

Chestnut:

let origin={
    x:1,
    y:2
}
let proxy=new Proxy(origin,{
    deleteProperty:(target, key)=>{
        if(key==='x')return
        delete target[key]
    }
})
delete proxy.x
delete proxy.y
console.log(proxy) // {x:1}
Copy after login

has: Determine whether a certain attribute is included

Syntax:

has(target, key)
Copy after login

target: Proxy object

key: Attribute to be judged

Chestnut:

let origin={
    x:1,
    y:2
}
let proxy=new Proxy(origin,{
    has:(target, key)=>{
        if(key==='x')return false
        return true
    }
})
console.log('x' in proxy) // false
console.log('y' in proxy) // true
Copy after login

ownKeys: Get own attribute value

  • Syntax:

    ownKeys(target)
    Copy after login
    • ##target

      : Proxy object

    Chestnut:
  • let origin={
        x:1,
        y:2
    }
    let proxy=new Proxy(origin,{
        ownKeys:(target)=>{
            return ['y']
        }
    })
    console.log(Object.getOwnPropertyNames(proxy)) // ['y']
    Copy after login

getPrototypeOf

:Get prototype

    Syntax:
  • getPrototypeOf(target)
    Copy after login

    • target

      : Proxy object

    chestnut
  • let origin={
        x:1,
        y:2
    }
    let proxy=new Proxy(origin,{
        getPrototypeOf:(target)=>{
            return null
        }
    })
    console.log(Object.getPrototypeOf(p)) // null
    Copy after login

setPrototypeOf

:Set prototype

    Syntax:
  • setPrototypeOf(target, prototype)
    Copy after login

    • target

      : Proxy object

    • prototype

      : The prototype to be set

    Chestnut
  • let origin={
        x:1,
        y:2
    }
    let proxy=new Proxy(origin,{
        setPrototypeOf:(target, prototype)=>{
            throw 'no'
        }
    })
    Object.setPrototypeOf(proxy, {}) //  Uncaught no
    Copy after login

defineProperty

: Set property description

    Syntax :
  • defineProperty(target, prop, descriptor)
    Copy after login

    • target

      : Proxy object

    • prop

      : To set the description Properties

    • descriptor

      : description

    chestnut
  • let origin={}
    let proxy=new Proxy(origin,{
        defineProperty:(target, prop, descriptor)=>{
            throw 'no'
        }
    })
    Object.defineProperty(proxy, 'x', {configurable: true}) //  Uncaught no
    Copy after login

getOwnPropertyDescriptor

:Get own property description

    Syntax:
  • getOwnPropertyDescriptor(target, prop)
    Copy after login
    • target: 代理对象

    • prop: 获取描述的属性

  • 栗子

    let origin={}
    let proxy=new Proxy(origin,{
        getOwnPropertyDescriptor:(target, prop)=>{
            throw 'no'
        }
    })
    Object.getOwnPropertyDescriptor(proxy, 'x') //  Uncaught no
    Copy after login

isExtensible:判断是否可扩展

  • 语法:

    isExtensible(target)
    Copy after login
    • target: 代理对象

  • 栗子

    let origin={}
    let proxy=new Proxy(origin,{
        isExtensible:(target)=>{
           return false
        }
    })
    console.log(Object.isExtensible(proxy)); // false
    Copy after login

preventExtensions :阻止扩展

  • 语法:

    preventExtensions(target)
    Copy after login
    • target: 代理对象

  • 栗子:

    let origin={}
    let proxy=new Proxy(origin,{
        preventExtensions:(target)=>{
            return false;
        }
    })
    console.log(Object.preventExtensions(proxy)); // Uncaught TypeError: 'preventExtensions' on proxy: trap returned falsish
    Copy after login

construct:构造

  • 语法:

    construct(target, argumentsList, newTarget)
    Copy after login
    • target: 代理对象

    • argumentsList: 参数列表

    • newTarget: 新对象

  • 栗子:

    let Origin=function(){}
    let OriginProxy=new Proxy(Origin,{
      construct: function(target, argumentsList, newTarget) {
          throw 'error'  
      }
    })
    new OriginProxy() // Uncaught error
    Copy after login

apply:调用

  • 语法:

    apply(target, thisArg, argumentsList)
    Copy after login

    target: 代理对象

    thisArg: 上下文

    argumentsList: 参数列表

  • 栗子:

    let origin=function(){}
    let proxy=new Proxy(origin,{
      apply: function(target, thisArg, argumentsList) {
        throw 'error'
      }
    })
    origin() // Uncaught error
    Copy after login



The above is the detailed content of Detailed introduction to proxies in es6 (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template