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

119
Views
Captador indexado en Javascript

Estoy usando Javascript directo (sin JQuery ni nada por el estilo, por favor). Implementé una clase que envuelve una matriz, por lo tanto:

 class Ctrls { _items = new Array(); constructor() { this._items = new Array(); } Add(oCtrl) { this._items.push( { key:oCtrl.Name, value:oCtrl } ); } Clear() { this._items = new Array(); } get Count() { return this._items.length; } get Item(index) { // get the index'th item. // If item is numeric, this is an index. // If item is a string, this is a control name if (Number.isInteger(index)) { return this._items(index).value; } else { item = this._items.find(element => (element.value.Name == index)); return item; } } get Items() { return this._items; // in case we desperately need to } }

Recibo un error en la carga de la página, en get Item(index) , que es Uncaught SyntaxError: Getter must not have any formal parameters . Vengo del mundo de C# y estoy buscando un equivalente de:

 public Ctrl Item(iIndex) { get { return _items[iIndex]; } }

¿Cómo indexo un getter en Javascript?


Editar (1): he tenido sugerencias para convertir get Item en una función, pero si cambio la definición a esto:

 function GetItem(index) // returns Ctrl { // get the index'th item. // If item is numeric, this is an index. // If item is a string, this is a control name if (Number.isInteger(index)) { return this._items(index).value; } else { item = this._items.find(element => (element.value.Name == index)); return item; } }

Recibo este error al cargar la página: Error de sintaxis Uncaught SyntaxError: Unexpected identifier en la function GetItem...


Editar (2): modificó lo anterior para leer:

 GetItem(index) // returns Ctrl { // get the index'th item. // If item is numeric, this is an index. // If item is a string, this is a control name if (Number.isInteger(index)) { return this._items(index).value; } else { item = this._items.find(element => (element.value.Name == index)); return item; } }

ya que las funciones dentro de las clases no usan la palabra clave de function , curiosamente. Esto ahora funciona. Gracias a todos.

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

0

"no puede pasar parámetros a getters en JS". Teóricamente: sí, no puedes hacer eso. Pero en la práctica: las funciones son ciudadanos de primera clase en JS, por lo que pueden ser argumentos o devolver valores de una función. Puedes hacerlo así:

 class GetterWithParameter { constructor() { this.array = ["index 0", "index 1", "index 2"] } get itemAtIndex() { return (idx) => this.array[idx] } } const getterWithParameter = new GetterWithParameter() const idx0 = getterWithParameter.itemAtIndex(0) const idx1 = getterWithParameter.itemAtIndex(1) const idx2 = getterWithParameter.itemAtIndex(2) console.log("item at index 0:", idx0) console.log("item at index 1:", idx1) console.log("item at index 2:", idx2)

Entonces, mientras que el getter no puede tener argumentos, puede devolver una función que puede recibir un argumento, y usar eso.

Por supuesto, el uso parece idéntico a definir una función en la clase que requiere el mismo argumento, pero aún así, está utilizando un getter .

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!