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

231
Views
cómo usar un mapa en evenlistener en javascript

mi código funciona con el mapa, m, hasta que lo uso en el detector de eventos haciendo clic en el botón, luego arroja un error y me dice que m.get() no es una función, sin embargo, funciona antes del evento de clic, ¿qué puede que hago para arreglar esto y ¿por qué sucede esto?

 'use strict'; const poll = { question: 'What is your favourite programming language?', options: ['0: JavaScript', '1: Python', '2: Rust', '3: C++'], // This generates [0, 0, 0, 0]. More in the next section 😃 answers: new Array(4).fill(0), registerNewAnswer(m){ const answer = Number(prompt(`${this.question}\n${this.options.join('\n')}`)); answer < 4 && answer >= 0 ? answer : alert('invalid entry try again'); //console.log(answer); m.set(this.options[answer],m.get(poll.options[answer]) + 1) } } const fun = function(arr){ let narr =[]; for(let i =0;i<arr.length;i++){ let temp = []; temp.push(arr[i]); temp.push(0); narr.push(temp); } return narr; } const m = new Map(fun(poll.options)); const pollPrompt = poll.registerNewAnswer.bind(poll); pollPrompt(m); document.querySelector('.poll').addEventListener('click',poll.registerNewAnswer.bind(poll));
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

El problema es cuando la devolución de llamada se ejecuta al click , m apunta al HTMLDOMNode y no al map

Puedes hacer lo siguiente:

 const poll = { question: 'What is your favourite programming language?', options: ['0: JavaScript', '1: Python', '2: Rust', '3: C++'], // This generates [0, 0, 0, 0]. More in the next section 😃 answers: new Array(4).fill(0), registerNewAnswer(m){ console.log('m', m); const answer = Number(prompt(`${this.question}\n${this.options.join('\n')}`)); answer < 4 && answer >= 0 ? answer : alert('invalid entry try again'); //console.log(answer); m.set(this.options[answer],m.get(poll.options[answer]) + 1) } } const fun = function(arr){ let narr =[]; for(let i =0;i<arr.length;i++){ let temp = []; temp.push(arr[i]); temp.push(0); narr.push(temp); } return narr; } const m = new Map(fun(poll.options)); const pollPrompt = poll.registerNewAnswer.bind(poll); pollPrompt(m); document.querySelector('.poll').addEventListener('click', () => { poll.registerNewAnswer(m) });
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!