关于javascript:JavaScript-手写题二

5次阅读

共计 2358 个字符,预计需要花费 6 分钟才能阅读完成。

类型判断
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 | string, char: string): 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 = [5, 3, 46, 7, 89, 45, 6, 78, 6, 4, 1, 8, 5, 2, 3, 7, 5, 6];
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 = [5, 3, 46, 7, 89, 45, 6, 78, 6, 4, 1, 8, 5, 2, 3, 7, 5, 6];
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 = [5, 3, 46, 7, 89, 45, 6, 78, 6, 4, 1, 8, 5, 2, 3, 7, 5, 6];
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 手写题二

正文完
 0