What is the agency model

Let’s take a real life example

As a user, we need to go to how to evaluate the quality of a house, how to deal with housing procedures and so on some tedious things? Obviously, users don’t want to do this. What the user cares about most is the result, the user asks for the house and the money that provides the equivalent value can get the satisfactory house, this is the result.

So who solves a series of tedious buying process for users? Of course, it is the “housing intermediary”! The function of real estate intermediary is to provide evaluation, transaction, agency, consultation and other services and after-care services for the trading objects in the supply and demand market of real estate development, operation and consumption.

Understand the definition of the agent pattern with the case

In cases where one object is unsuitable or cannot refer directly to another object, a proxy object can mediate between the client and the target object.

Agent mode is to provide an agent for other users, users do not need to know the specific process of buying a house, and users should be concerned about how to obtain satisfactory results, what the agent needs to do is to complete the process of buying a house.

What is a Proxy

Proxy is used to modify the default behavior of some operations. An “interception” layer is set up in front of the target object. All external access to the object must pass this layer to intercept, so it provides a mechanism to filter and rewrite the external access. — Introduction to ES6 Standards — Chapter 12

There are many interception operations supported by Proxy, currently only for get(target, propKey, receiver) and set(target, propKey, value, receiver).

  1. The get method intercepts the reading of object properties.
  2. Set method: intercepts the setting of an object property.

get(target, propKey, receiver)

Define a Person object that will be propped up by a Proxy and accessed by the Proxy instance object.

var person = {
    name: "kongsam".age: 21.hobbies: [
        "Watch anime"."Ride"."Play a game."]}Copy the code

Instantiate a Proxy object that intercepts external operations on the Person object.

var proxy = new Proxy(person, {
    get: function (target, property) {
          // Print the target and property to see what's inside.
          console.log("target = ", target);
          console.log("property = ", property);
          // Determine whether the attributes of the object accessed by the outside world exist in the target object.
          if (property in target) {
                return target[property];
          } else {
                // Throw an exception if the object properties accessed by the outside world do not exist in the target object.
                throw new ReferenceError('Property "' + property + '" 不存在。'); }}});Copy the code

When I do the proxy.name operation, since the Person object is already represented by the proxy, every time I access a property that exists in the Person object through the Proxy instance object, I call the get method, which intercepts the reading of the object’s property.

Get: The information received by target and property in function (target, property) is shown in the figure

There are no exceptions to accessing properties that exist in the Person object through this proxy object, but what happens if you access properties that don’t exist?

What is it that throws an exception when you access a property that doesn’t exist?

This is because any outside access to the Person object must first go through the interceptor layer set by the Proxy, and the interceptor layer provides a mechanism to filter and override outside access.

// Determine whether the attributes of the object accessed by the outside world exist in the target object.
if (property in target) {
    return target[property];
} else {
    // Throw an exception if the object properties accessed by the outside world do not exist in the target object.
    throw new ReferenceError('Property "' + property + '" 不存在。');
}
Copy the code

The if statement is the operation of the interception layer, that is, to filter and rewrite external access. If not, accessing a nonexistent property returns undefined.

set(target, propKey, value, receiver)

It’s still a Person object, and I have a new requirement, which is that when I modify the age property, the value cannot exceed 150 and be an integer.

Added the set method to the Proxy object.

var proxy = new Proxy(person, {
    set: function (target, property, value) {
          // Print target, property, and value to see what's inside.
          console.log("target = ", target);
          console.log("property = ", property);
          console.log("value = ", value);
          if (property === "age") {
                if (!Number.isInteger(value)) {
                  throw new TypeError("Value of age is not an integer!");
                }
                if (value > 150) {
                  throw new RangeError("Age must not be greater than 150!"); }}}});Copy the code

When I execute proxy.age = 100, I receive the following information for each of the three parameters of set.

The set method is used to intercept an assignment to a property. What happens if MY assignment to age does not meet the criteria?

Obviously, an exception will be thrown.

conclusion

Proxy is the interception layer, you give an object that’s intercepted, and the outside world has to access that object through the interception layer, which is the instance object of the Proxy. Proxy is used to filter and rewrite external access. For example, certain conditions must be met when assigning values.

There are many methods in the proxy object, such as has, deleteProperty, ownKeys, getOwnPropertyDescriptor, etc., which are used to intercept different situations.