Should.js 使用问题记录

Should.js 使用问题记录

should.extend 方法

should 的基本原理就是在 Object.prototype 上定义一个 should 对象,拦截 get,进行特殊处理。

should$1.extend = function (propertyName, proto) {
  propertyName = propertyName || "should";
  proto = proto || Object.prototype;

  var prevDescriptor = Object.getOwnPropertyDescriptor(proto, propertyName);

  Object.defineProperty(proto, propertyName, {
    set: function () {
    },
    get: function () {
      return should$1(isWrapperType(this) ? this.valueOf() : this);
    },
    configurable: true
  });

  return {name: propertyName, descriptor: prevDescriptor, proto: proto};
};

should.noConflict 方法

删除掉对象中 proto 中的 should 对象,取消 should 对属性读取的拦截

should$1.noConflict = function (desc) {
  desc = desc || should$1._prevShould;

  if (desc) {
    delete desc.proto[desc.name];

    if (desc.descriptor) {
      Object.defineProperty(desc.proto, desc.name, desc.descriptor);
    }
  }
  return should$1;
};

导致的问题

如果代码中有一个对象中,想要额外增加一个 should 的属性,正好重名,那就永远访问也不能覆盖掉这个should。 这时就会导致代码中缺少了 should 中的对象,导致代码出错。

require('should')

const a = {
  a: 1,
  b: 2,
}

// 此时的 should 是 should.Assertion 对象,无法重新赋值
a.should = {
  a: 1,
  b: 2
}

console.log(a)
// 打印结果
// {a: 1,b: 2}

解决办法

const should = require('should')

// 关闭 should 的拦截,保证这次测试可以使用
should.noConflict()
const a = {
  a: 1,
  b: 2,
}

a.should = {
  a: 1,
  b: 2
}

console.log(a)
// { a: 1, b: 2, should: { a: 1, b: 2 } }

// 重新开启 should 的拦截,保证后续的测试可以继续使用 should
should.extend()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值