Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

面试题复习 #11

Open
xieyezi opened this issue Mar 4, 2021 · 0 comments
Open

面试题复习 #11

xieyezi opened this issue Mar 4, 2021 · 0 comments
Labels
documentation Improvements or additions to documentation

Comments

@xieyezi
Copy link
Owner

xieyezi commented Mar 4, 2021

import "./styles.css";
import { useEffect } from "react";

// call 实现
// eslint-disable-next-line no-extend-native
Function.prototype.Mycall = function (context) {
  var context = context || window;
  context.fn = this;
  var args = [...arguments].slice(1);
  var result = context.fn(...args);
  delete context.fn;
  return result;
};

// apply 实现
// eslint-disable-next-line no-extend-native
Function.prototype.MyApply = function (context) {
  var context = context || window;
  context.fn = this;
  var args = [...arguments][1];
  var result = null;
  if (args) {
    result = context.fn(...args);
  } else {
    result = context.fn();
  }
  delete context.fn;
  return result;
};

// bind实现
// eslint-disable-next-line no-extend-native
Function.prototype.MyBind = function (context) {
  var self = this;
  let args = [...arguments].slice(1);
  return function () {
    let bindArgs = [...arguments].slice(0);
    let allArgs = args.concat(bindArgs);
    return self.apply(context, allArgs);
  };
};

// new 实现
function myNew() {
  let obj = {};
  let Cons = arguments[0];
  let args = [...arguments].slice(1);
  // console.log(Cons);
  // console.log(args);
  obj.__proto__ = Cons.prototype;
  let res = Cons.call(obj, ...args);
  return typeof res === "object" ? res : obj;
}

// 寻找数组中第k大的数
function findK(nums, k) {
  // 1.全排列,复杂度,O(nlogN)
  // nums = nums.sort((a, b) => b - a);
  // return nums[k - 1];
  // 2. 冒泡排序,复杂度,n*k
  for (let i = 0; i < k; i++) {
    for (let j = 0; j < nums.length - 1; j++) {
      if (nums[j] > nums[j + 1]) {
        [nums[j], nums[j + 1]] = [nums[j + 1], nums[j]];
      }
    }
  }
  return nums[nums.length - k];
}

//形式1:sum(1).sumof()
// 形式2:sum(1,2).sumof()
// 形式3:sum(1)(2, 3).sumof()
// 最终求得的结果,需要是各个出现的数字的和。

function sum(...lastArgs) {
  console.log("lastArgs", lastArgs);
  var callback = function (...args) {
    console.log("args", args);
    return sum(...[...lastArgs, ...args]);
  };
  callback.sumof = function () {
    return lastArgs.reduce((aggregated, number) => aggregated + number, 0);
  };
  return callback;
}


// 防抖实现 debunce
function debunce(fn,delay){
  let timer; 
  return function(...args) {
    if(timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(() => {
      fn.apply(this,args)
    }, delay);
  }
}

// 节流实现
function throotle(fn,delay){
  let timer
  return function(...args) {
    if(!timer) {
      timer = setTimeout(()=>{
        timer = null
        fn.apply(this,args)
      },delay)
    }
  }
}

export default function App() {
  let foo = {
    value: 1
  };

  function bar(name, age) {
    console.log(name);
    console.log(age);
    console.log(this.value);
  }

  function Person(name, age) {
    this.name = name;
    this.age = age;
  }
  Person.prototype.sayName = function () {
    console.log(this.name + "hhh");
  };

  useEffect(() => {
    // bar.Mycall(foo, "xieyezi", 25);
    // bar.MyApply(foo, ["xieyezi", 25]);
    // let bindRes = bar.bind(foo, "xieyezi");
    // bindRes(19);
    // let a = myNew(Person, "xieyezi", 25);
    // console.log(a.sayName());
    // let k = findK([3, 2, 1, 5, 6, 4], 2);
    // console.log(k);
    let total = sum(1)(2, 3).sumof();
    console.log(total);
  }, []);

  return (
    <div className="App">
      <h1>Hello CodeSandbox</h1>
      <h2>Start editing to see some magic happen!</h2>
    </div>
  );
}
@xieyezi xieyezi added the documentation Improvements or additions to documentation label Mar 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

1 participant