JavaScript 手写题(二)

73 阅读2分钟

类型判断

const my_type_of = (data) => {
  const toString = Object.prototype.toString
  // 方式一
  const type = toString.call(data)
  // 方式二
  const type2 = toString.call(data).slice(8, -1)
  // 方式三
  const type3 = toString.call(data).split(' ')[1].slice(0,-1)
}

千分位切割

const thousandth = ( num: number | stringcharstring ): string => {
  const str = num.toString()
  const len = str.length
  if( len < 3 return num
  // 获取余数
  const remainder = len % 3
  // 如果余数大于零,说明不能被整除
  if(remainder > 0) {
    return str.slice(0, remainder) + char + str.match(/\d{3}/g).join(',')
  }
  return str.match(/\d{3}/g).join(',')
}

new 实现

new做了什么?

1、创建一个新的对象

2、将新对象的原型  proto  指向构造函数的原型 prototype (改变 this 指向)

3、执行构造函数,将属性和方法添加到当前 this

4、判断构造函数是否有返回对象,如果有,就返回构造函数的对象,如果没有就返回新建的对象

实现:

function _new () {
  const target = {}
  const [constructor,...args] = [...arguments];
  target._proto_ = constructor.prototype
  const result = constructor.prototype
  if (result && typeof result == 'object' || typeof result == 'function') {
    return result
  }
  return target
}

数组排序

冒泡排序

const arr = [5346789456786418523756];
for (let i = 0; i < arr.length - 1; i++) {
  for (let j = 0; j < arr.length - i - 1; j++) {
    if (arr[j] > arr[j + 1]) {
      [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
    }
  }
}

双向冒泡排序

const arr = [5346789456786418523756];
function twoWay(arr) {
        let small = 0;
        let large = arr.length;
        while (small < large) {
          // 该轮是否发生位置交换
          let isChange = false;

          // 找大值
          for (let i = small; i < large - 1; i++) {
            if (arr[i] > arr[i + 1]) {
              [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]];
            }
          }
          large--;
          // 找小值
          for (let j = large - 1; j > small; j--) {
            if (arr[j] < arr[j - 1]) {
              [arr[j], arr[j - 1]] = [arr[j - 1], arr[j]];
              // isChange = true;
            }
          }
          small++;
        }
        return arr;
      }

快速排序

const arr = [5346789456786418523756];
function fast(arr) {
  if (arr.length <= 1) return arr;
  const right = [],
        left = [];
  const midIndex = (arr.length / 2) | 0;
  const midVal = arr.splice(midIndex, 1)[0];
  for (let i = 0; i < arr.length; i++) {
    arr[i] < midVal ? right.push(arr[i]) : left.push(arr[i]);
  }
  return [...fast(right), midVal, ...fast(left)];
}
console.log(fast(arr));

生成随机数

// [3 - 5] 随机数
// 因为
(Math.random() * 1) | 0 //  [0]
(Math.random() * 2) | 0 //  [0, 1]
(Math.random() * 3) | 0 //  [0, 2]

((Math.random() * 1) | 0) + 1 //  [1]
((Math.random() * 2) | 0) + 1 //  [1, 2]
((Math.random() * 3) | 0) + 1 //  [1, 3]

((Math.random() * 1) | 0) + 2 //  [2]
((Math.random() * 2) | 0) + 2 //  [2, 3]
((Math.random() * 3) | 0) + 2 //  [2, 4]

((Math.random() * 1) | 0) + 3 //  [3]
((Math.random() * 2) | 0) + 3 //  [3, 4]
((Math.random() * 3) | 0) + 3 //  [3, 5]

// 所以
// 生成随机数公式为:
 Math.random() * (5 - 3 + 1) + 3

// 即:
 Math.random() * (max - min + 1) + min

// 取整得:
( Math.random() * (max - min + 1) + min) | 0

function random(min, max) {
  return ( Math.random() * ( max - min + 1 ) + min ) | 0
  // return Marh.floor( Math.random() * ( max - min + 1 ) + min )
}

传送门(去点点关注吧~):JavaScript 手写题(二)