Use node-delegates to learn from the source

node-delegates is TJ a simple gadget god written, only the source line 157, functions to be entrusted to the operation of an external object received internal processing properties, it can be understood as internal attributes of an object exposed to the outside speaking, simplification we need to write code.

Installation and use of the code can be found in the source repository, this is mainly speak about API.

API

Delegate(proto, prop)

Creating a delegator for instance, for some operations the commissioned proto received prop attributes to it for processing.

Delegate.auto(proto, targetProto, targetProp)

The key targetProp included automatically determine the type of the corresponding property on the agent to targetProto proto. It may be getter, setter, value or method.

Delegate.prototype.method(name)

Add a proto called on the object namefunction, call the function equivalent to the function name on the call proto prop property.

Delegate.prototype.getter(name)

Add a getter to proto objects, visit the getter to access prop of proto corresponding getter.

Delegate.prototype.setter(name)

With the getter.

Delegate.prototype.access(name)

While adding a getter and setter in a proto, proto.prop point corresponding properties.

Delegate.prototype.fluent(name)

access The special form.

delegate(proto, 'request')
  .fluent('query')

// getter
var q = request.query();

// setter (chainable)
request
  .query({ a: 1 })
  .query({ b: 2 });

Source Reading

/**
 * Expose `Delegator`.
 */

// 暴露 Delegator 构造函数
module.exports = Delegator;

/**
 * Initialize a delegator.
 * 构造一个 delegator 实例
 * @param {Object} proto 外部对象,供外部调用
 * @param {String} target 外部对象的某个属性,包含具体处理逻辑
 * @api public
 */

function Delegator(proto, target) {
  // 如果没有使用 new 操作符调用构造函数,则使用 new 构造
  if (!(this instanceof Delegator)) return new Delegator(proto, target);
  // 构造实例属性
  this.proto = proto;
  this.target = target;
  this.methods = [];
  this.getters = [];
  this.setters = [];
  this.fluents = [];
}

/**
 * Automatically delegate properties
 * from a target prototype
 * 根据 targetProp 自动委托,绑定一个属性到 Delegator 构造函数
 * @param {Object} proto 接受请求的外部对象
 * @param {object} targetProto 处理具体逻辑的内部对象
 * @param {String} targetProp 包含要委托的属性的对象
 * @api public
 */

Delegator.auto = function(proto, targetProto, targetProp){
  var delegator = Delegator(proto, targetProp);
  // 根据 targetProp 获取要委托的属性
  var properties = Object.getOwnPropertyNames(targetProto);
  // 遍历所有要委托的属性
  for (var i = 0; i < properties.length; i++) {
    var property = properties[i];
    // 获取 targetProto 上对应属性的 descriptor
    var descriptor = Object.getOwnPropertyDescriptor(targetProto, property);
    // 如果当前属性的 get 被重写过,就作为 getter 委托(使用 __defineGetter__ 或者 Object.defineProperty 指定 getter 都会重写 descriptor 的 get 属性)
    if (descriptor.get) {
      delegator.getter(property);
    }
    // 同 get,如果 set 被重写过,那就作为 setter 委托
    if (descriptor.set) {
      delegator.setter(property);
    }
    // 如果当前 property 具有 value,那么判断是函数还是普通值
    if (descriptor.hasOwnProperty('value')) { // could be undefined but writable
      var value = descriptor.value;
      if (value instanceof Function) {
        // 是函数就进行函数委托
        delegator.method(property);
      } else {
        // 是普通值就作为 getter 委托
        delegator.getter(property);
      }
      // 如果这个值可以重写,那么继续进行 setter 委托
      if (descriptor.writable) {
        delegator.setter(property);
      }
    }
  }
};

/**
 * Delegate method `name`.
 * 
 * @param {String} name
 * @return {Delegator} self
 * @api public
 */

Delegator.prototype.method = function(name){
  var proto = this.proto;
  var target = this.target;
  this.methods.push(name);

  // 在 proto 上定义一个 name 的方法
  proto[name] = function(){
    // 实际还是调用的 proto[target][name],内部的 this 还是指向 proto[target]
    return this[target][name].apply(this[target], arguments);
  };

  return this;
};

/**
 * Delegator accessor `name`.
 *
 * @param {String} name
 * @return {Delegator} self
 * @api public
 */

Delegator.prototype.access = function(name){
  // 同时定义 getter 和 setter
  return this.getter(name).setter(name);
};

/**
 * Delegator getter `name`.
 * 委托 name getter
 * @param {String} name
 * @return {Delegator} self
 * @api public
 */

Delegator.prototype.getter = function(name){
  var proto = this.proto;
  var target = this.target;
  this.getters.push(name);

  // 使用 __defineGetter__ 绑定 name getter 到 proto
  proto.__defineGetter__(name, function(){
    // 注意 this 指向 proto 本身,所以 proto[name] 最终访问的还是 proto[target][name]
    return this[target][name];
  });

  // 此处 this 指向 delegator 实例,构造链式调用
  return this;
};

/**
 * Delegator setter `name`.
 * 在 proto 上委托一个 name setter
 * @param {String} name
 * @return {Delegator} self
 * @api public
 */

Delegator.prototype.setter = function(name){
  var proto = this.proto;
  var target = this.target;
  this.setters.push(name);

  // 通过 __defineSetter__ 方法指定一个 setter 到 proto
  proto.__defineSetter__(name, function(val){
    // 注意 this 指向 proto 本身,所以对 proto[name] 设置值即为为 proto[target][name] 设置值
    return this[target][name] = val;
  });

  // 返回自身实现链式调用
  return this;
};

/**
 * Delegator fluent accessor
 *
 * @param {String} name
 * @return {Delegator} self
 * @api public
 */

Delegator.prototype.fluent = function (name) {
  var proto = this.proto;
  var target = this.target;
  this.fluents.push(name);

  proto[name] = function(val){
    // 如果 val 不为空,那么就作为 setter 使用
    if ('undefined' != typeof val) {
      this[target][name] = val;
      // 完事后返回 proto 自身,实现链式调用
      return this;
    } else {
      // 如果 val 未定义,那么作为 getter 使用,返回具体的值
      return this[target][name];
    }
  };

  return this;
};

Specific case

The reason is because the library to look at look at koathe source code to see when using this library in koaby using node-delegatesthe context.requestand context.responseproperties on both entrusted to contextitself. So we can use directly context.query, context.statusto operate, simplifies the code we write.

koa source location link: https: //github.com/koajs/koa/blob/b7fc526ea49894f366153bd32997e02568c0b8a6/lib/context.js#L191

to sum up

  • By __defineGetter__and __defineSetter__can be set getter and setter, but MDN display both API has been deprecated, github also issue has already been raised and pr. Further, by setting both the getter and setter the API, an internal transfer function of this point to the original properties, such as:

    let a = { nickName: 'HotDog' }
    a.__defineGetter__('name', function() {
      return this.nickName // 此处 this 仍然指向 a
    })
  • Learning the delegation mode, the external object may be received specific processing operation commissioned to internal attributes (or other objects).

Guess you like

Origin www.cnblogs.com/DM428/p/11069522.html