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

214
Views
Javascript Custom Array.prototype.method registra 'no definido'

Entonces, nunca antes había hecho esto, tratando de agregar un método al Array.prototype. Consulte la declaración de console.log a continuación para conocer el uso. Me sigue diciendo que el método no está definido. No puedo entender lo que estoy haciendo mal. ¡Ayuda!

Mi mejor conclusión / conjetura hasta ahora es que "esto" se refiere al objeto global, y eso lo está arruinando de alguna manera. Pero cómo arreglar eso, ni idea. :(

 const solution = input =>{ Object.defineProperty( Array.prototype, 'polyReverse', { value: () => this ? (polyReverse(this.substr(1)) + this[0]) : (this), configurable: true, writable: true } ); console.log("string".split("").polyReverse().join("")); }; /***** * * ReferenceError: polyReverse is not defined * at Array.value (main.js on line 4:20) * * *///////

NOTA: También probé esto por el valor del valor..

 value: () => this ? (this.substr(1).polyReverse() + this[0]) : (this),

y esto ...

 value: () => this ? (this.polyReverse(this.substr(1)) + this[0]) : (this),

sin suerte

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

0

Intenté lo siguiente y pude resolver el problema de not defined

 Array.prototype.polyReverse = function(value) { return this ? (this.substr(1).polyReverse() + this[0]) : (this) }; console.log("string".split("").polyReverse().join(""));

Más allá de esto, su lógica parece tener algún problema y arroja un error. No estoy seguro de lo que está tratando de lograr con polyReverse , por lo que sería la mejor persona para arreglar esa lógica.

Dado que preguntó específicamente sobre el problema not defined , el fragmento anterior debería resolver su problema y ayudarlo a continuar con la solución de la lógica.

about 4 years ago · Juan Pablo Isaza Report

0

Entonces, según los consejos de personas útiles aquí, pude solucionar el problema como todos dijeron, no use funciones de flecha. ¡SÍ! Además, ¡tomé nota de cómo extender los métodos nativos es MALO! Y haré una subclase para hacer esto ahora.

Gracias a todos por la ayuda.

Codigo de trabajo...

 const solution = input =>{ Object.defineProperty( Array.prototype, 'polyReverse', { value: function() { console.log(this); return (this.length > 0 ? (this.join("").substr(1).split("").polyReverse() + this[0]) : (this) )}, configurable: true, writable: true } ); console.log("string".split("").polyReverse()); };
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!