Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
¿Cómo puedo encadenar un método javascript con un valor también?

Entonces, si quiero hacer algo como cinco().más().cinco().equals() // esto debería devolver 10 ¿Cómo puedo lograr esto? Entiendo el encadenamiento de métodos cuando las funciones devuelven otra función, pero ¿qué pasa cuando quieres usar un valor en la siguiente función de la cadena?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

 class Stack{ constructor(state=0){ this.state = state; this.op = null; } pushVal(v){ switch(this.op){ case '+': this.state += v; break; case '-': this.state -= v; break; default: this.state = v; break; } } plus(){ this.op = '+'; return this; } minus(){ this.op = '-'; return this; } equals(){ return this.state; } } consts = { five: 5, six: 6, seven: 7 }; for(const n in consts){ Stack.prototype[n] = function (){ this.pushVal(consts[n]) return this; } } five = () => new Stack(5) /// Now we can run what you want console.log(five().plus().five().equals()) // and some extension of it console.log(five().plus().five().minus().seven().equals())

about 4 years ago · Juan Pablo Isaza Report

0

El Chaining es un concepto en JS en el que podemos llamar repetidamente a las propiedades de un objeto.

Lo que puedes hacer, si solo se trata de encadenar,

 let obj = { num:0, get(){ console.log(this.num); return this; }, set(num){ this.num = num; return this; }, add(a){ this.num = this.num+a; return this; }, subtract(a){ this.num = this.num-a; return this; }, divide(a){ this.num = this.num/a; return this; }, multiply(a){ this.num = this.num*a; return this; } } obj.set(5).add(2).multiply(3).divide(7).add(3).get();

The most important part in chaining is devolver este

Puede leer más sobre esto en: https://medium.com/technofunnel/javascript-function-chaining-8b2fbef76f7f

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!