Array && String 添加原型链小方法

90 阅读1分钟

Array

  1. Array duplicator 复制原数组
Array.prototype.duplicator = function () {
  let _target = this;
  let res = !_target.length ? []:_target.concat(_target);
  return res
}

String

1.hidePhoneNum 手机号码隐藏中间四位

String.prototype.hidePhoneNum = function () {
  let _target = this;
  let res = _target.length !== 11 ? 'error phone number' : _target.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
  return res
}