GithubHelp home page GithubHelp logo

Comments (7)

Xiaolong96 avatar Xiaolong96 commented on May 1, 2024 4
var pipe = function (value) {
  let stack = [];
  let proxy = new Proxy(
    {},
    {
      get(target, prop, receiver) {
        if (prop === "get") {
          return stack.reduce((x, fn) => {
            return fn(x);
          }, value);
        }
        stack.push(window[prop]);
        // !关键
        return proxy;
      },
    }
  );
  return proxy;
};

var double = (n) => n * 2;
var pow = (n) => n * n;
var reverseInt = (n) => n.toString().split("").reverse().join("") | 0;

pipe(3).double.pow.reverseInt.get; // 63

from fe-interview.

chinbor avatar chinbor commented on May 1, 2024 1
class  Calculator {
  constructor(val) {
    this.value = val
  }

  add(val) {
    this.value += val

    return this
  }

  sub(val) {
    this.value -= val

    return this
  }

  mul(val) {
    this.value *= val

    return this
  }

  div(val) {
    this.value /= val

    return this
  }

  toString() {
    return this.value
  }
}

const calculator = new Calculator(2)

console.log(+calculator.add(3).sub(2).mul(10).div(2))

from fe-interview.

Genzhen avatar Genzhen commented on May 1, 2024

链式调用的核心就在于调用完的方法将自身实例返回
1)示例一

function Class1() {
    console.log('初始化')
}
Class1.prototype.method = function(param) {
    console.log(param)
    return this
}
let cl = new Class1()
//由于new 在实例化的时候this会指向创建的对象, 所以this.method这个方法会在原型链中找到。
cl.method('第一次调用').method('第二次链式调用').method('第三次链式调用')

2)示例二

var obj = {
    a: function() {
        console.log("a");
        return this;
    },
    b: function() {
        console.log("b");
        return this;
    },
};
obj.a().b();

3)示例三

// 类
class Math {
    constructor(value) {
        this.hasInit = true;
        this.value = value;
        if (!value) {
            this.value = 0;
            this.hasInit = false;
        }
    }
    add() {
        let args = [...arguments]
        let initValue = this.hasInit ? this.value : args.shift()
        const value = args.reduce((prev, curv) => prev + curv, initValue)
        return new Math(value)
    }
    minus() {
        let args = [...arguments]
        let initValue = this.hasInit ? this.value : args.shift()
        const value = args.reduce((prev, curv) => prev - curv, initValue)
        return new Math(value)
    }
    mul() {
        let args = [...arguments]
        let initValue = this.hasInit ? this.value : args.shift()
        const value = args.reduce((prev, curv) => prev * curv, initValue)
        return new Math(value)
    }
    divide() {
        let args = [...arguments]
        let initValue = this.hasInit ? this.value : args.shift()
        const value = args.reduce((prev, curv) => prev / (+curv ? curv : 1), initValue)
        return new Math(value)
    }
}

let test = new Math()
const res = test.add(222, 333, 444).minus(333, 222).mul(3, 3).divide(2, 3)
console.log(res.value)

// 原型链
Number.prototype.add = function() {
    let _that = this
    _that = [...arguments].reduce((prev, curv) => prev + curv, _that)
    return _that
}
Number.prototype.minus = function() {
    let _that = this
    _that = [...arguments].reduce((prev, curv) => prev - curv, _that)
    return _that
}
Number.prototype.mul = function() {
    let _that = this
    _that = [...arguments].reduce((prev, curv) => prev * curv, _that)
    return _that
}
Number.prototype.divide = function() {
    let _that = this
    _that = [...arguments].reduce((prev, curv) => prev / (+curv ? curv : 1), _that)
    return _that
}
let num = 0;
let newNum = num.add(222, 333, 444).minus(333, 222).mul(3, 3).divide(2, 3)
console.log(newNum)

from fe-interview.

GolderBrother avatar GolderBrother commented on May 1, 2024

class LinkCall {
constructor(name) {
this.name = name;
}
call(params) {
console.log(params);
this.params = params;
// 核心点在于最后返回实例本身
return this;
}
}
const linkCall = new LinkCall('james');
linkCall.call('call one').call('call two').call('call three');

// call one
// call two
// call three

from fe-interview.

chengazhen avatar chengazhen commented on May 1, 2024
class Person {
      handle() {
        console.log('先洗手')
        return this
      }
      eat() {
        console.log('在吃饭');
        return this
      }
      drink() {
        console.log('去喝水');
        return this
      }
      say() {
        console.log('聊聊天')
      }
    }
    const person = new Person();
    person.handle().eat().drink().say()
//先洗手 index.html:35 在吃饭 index.html:39 去喝水 index.html:43 聊聊天

from fe-interview.

zizxzy avatar zizxzy commented on May 1, 2024

链式调用的关键在于返回自身this,便于后面继续调用

from fe-interview.

Kisthanny avatar Kisthanny commented on May 1, 2024
/**
 * 实现链式调用
 * 实例的方法返回实例本身就可以链式调用
 */
// 四则运算类
class ASMD {
  constructor(value) {
    this.value = value;
  }
  add(num) {
    console.log("add", num);
    this.value += num;
    return this;
  }
  subtract(num) {
    console.log("subtract", num);
    this.value -= num;
    return this;
  }
  multiply(num) {
    console.log("multiply", num);
    this.value *= num;
    return this;
  }
  divide(num) {
    console.log("divide", num);
    this.value /= num;
    return this;
  }
}

const num = new ASMD(0);
const result = num.add(10).subtract(2).divide(4).multiply(100).value;
console.log(result);

from fe-interview.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.