throttle

  1. function throttle(fn, threshhold) {
  2. var last, timerId;
  3. threshhold || (threshhold = 250);
  4. return function() {
  5. var now = Date.now();
  6. if(last && now - last < threshhold) {
  7. clearTimeout(timerId);
  8. timerId = setTimeout(() => {
  9. fn.apply(this, arguments);
  10. }, threshhold)
  11. } else {
  12. last = now;
  13. fn.apply(this, arguments);
  14. }
  15. }
  16. }

debonce

去抖 连续触发

  1. function debounce(fn, interval) {
  2. var timerId = null;
  3. return function() {
  4. clearTimeout(timerId);
  5. timerId = setTimeout(() => {
  6. fn.apply(this, arguments)
  7. }, interval)
  8. }
  9. }

call apply bind

主要是借助对象的方法调用可以指定this

简单版本

  1. var foo = {
  2. value: 1,
  3. bar: function() {
  4. console.log(this.value)
  5. }
  6. }
  7. foo.bar() // 1

进阶

  1. // 修改 prototype 版本
  2. Function.prototype.call2 = function(content = window, ...args) {
  3. content.fn = this;
  4. let result = content.fn(...args);
  5. delect content.fn;
  6. return result;
  7. }
  8. // 函数版本
  9. function call2 (context = window, fn) {
  10. context.fn = fn;
  11. let args = [...arguments].slice(2);
  12. let result = context.fn(...args);
  13. delete context.fn;
  14. return result;
  15. }
  16. // apply 第2个参数是数组
  17. Function.prototype.apply2 = function(content = window, args) {
  18. content.fn = this;
  19. let result = content.fn(...args);
  20. delect content.fn;
  21. return result;
  22. }
  1. // bind
  2. Funtion.prototype.bind = function(obj) {
  3. if(type of this !== 'function') {
  4. return;
  5. }
  6. var _self = this;
  7. var args = [].slice.call(arguments, 1);
  8. return function() {
  9. return _self.apply(obj, args.concat([].slice.call(arguments)))
  10. }
  11. }

斐波那契

  1. // R1
  2. let fib = (function() {
  3. let memory = []
  4. return function(n) {
  5. if(memory[n] !== undefined) {
  6. return memory[n]
  7. }
  8. return memory[n] = (n === 0 || n === 1) ? n : fibonacci(n-1) + fibonacci(n-2)
  9. }
  10. })()
  11. // R2
  12. function fib(n) {
  13. if(n === 0 || n === 1) return n;
  14. return fib(n - 2) + fib(n - 1);
  15. }

继承

  1. function Dog(color) {
  2. Animal.apply(this, arguments);
  3. this.name = 'dog';
  4. }
  5. /* 注意下面两行 */
  6. Dog.prototype = Object.create(Animal.prototype);
  7. Dog.prototype.constructor = Dog;

柯里化

  1. function currying(fn) {
  2. const argArr = [];
  3. let closure = function(...args) {
  4. if(args.length > 0) {
  5. argArr = [...argArr, ...args];
  6. return closure;
  7. }
  8. return fn(...argArr);
  9. }
  10. return closure;
  11. }

new

  1. function newOps (ctor) {
  2. if(typeof ctor !== 'function') {
  3. throw new Error('the first param must be a function');
  4. }
  5. const new0bj = Object.create(ctor.prototype);
  6. const args = [].slice.call(arguments, 1);
  7. const ctorReturnResult = ctor.apply(newObj, args);
  8. if((typeof ctorReturnResult === 'object' && typeof ctorReturnResult !== null) || typeof ctorReturnResult === 'function') {
  9. return ctorReturnResult;
  10. }
  11. return newObj;
  12. }