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

216
Views
Do I can Symbol.iterator in Object by prototype?"JavaScript"

Why I can't add Symbol.iterator into Object like that

Object.prototype[Symbol.iterator];

let obj = {num: 1 , type : ' :) '}
for (let p of obj) {
  console.log(p);
}
// TypeError: obj is not iterable
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You have to assign a generator function to Object.prototype[Symbol.iterator].

Object.prototype[Symbol.iterator] = function*() {
  for (let k in this) {
    yield this[k];
  }
};

let obj = {
  num: 1,
  type: ' :) '
}
for (let p of obj) {
  console.log(p);
}

about 4 years ago · Juan Pablo Isaza Report

0

Because you didnt actually assign anything to Object.prototype[Symbol.iterator] - if you do assign an iterator function that does somethign sensible I think it might work as you expected

Object.prototype[Symbol.iterator] = function*(){
  for(let entry of Object.entries(this))
     yield entry
};

let obj = {num: 1 , type : ' :) '}
for (let [k,v] of obj) {
  console.log(k,v);
}

// another way
console.log(...obj)

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!