20200218

@xiaojingzhao

Plan

一小时:你不知道的 js

Notes

  • this 的软绑定
Function.prototype.softBind = function(obj) {
  var fn = this;
  var curried = [].slice.call(arguments, 1);
  var bound = function() {
    return fn.apply(
      !this || this === (window || global) ? obj : this,
      curried.concat.apply(curried, arguments)
    );
  };

  bound.prototype = Object.create(fn.prototype); // 保证 bound函数 不丢失原型链。bound.prototyp.__proto__ === fn.prototype
  return bound;
};
  • 为什么 "typeof null" 返回的是 "object"? 因为在 js 中,二进制前三位都是 0 的话,会被判断为 object 类型,而 null 的二进制表示全是 0,所以 null 也被判断为 object

More