evantianx / Bloooooooog

Place to record what I thought and learned
0 stars 0 forks source link

可能有那么一点用系列 #66

Open evantianx opened 6 years ago

evantianx commented 6 years ago

获取全局对象

const global = (function() {
  return this
})()

hasOwnProperty()

// 缓存 hasOwnProperty 方法
const hasOwn = object.prototype.hasOwnProperty;

for (let i in man) {
  if (hasOwn.call(man, i)) {
    // do something
  }
}

为对象原型添加方法

if (typeof object.prototype.myMethod !== 'function') {
  object.prototype.myMethod = function() {
    // do something
  }
}

获取介于两个整数之间的整数,包括边界

function getRandomIntInclusive(min, max) {
  return Math.floor(Math.random() * (max- min + 1) + min)
}