JavaScript: Why does the "for in" loop pick variables from __proto__?

for...in goes over all of the object properties without distinguishing between properties on the object itself or any of its ancestors.

In order to go over only properties defined on the object itself, you can use Object.prototype.hasOwnProperty:

const obj = { 4: 15, 10 : 41, 11 : 46, 12 : 51, 20 : 74 }
for( item in obj ) {
  if(obj.hasOwnProperty(item) {
    foo( obj[item] );
  }
}

// will ignore the trigger func and everything else defined on any prototype

for..in iterates over all enumerable properties anywhere on the prototype chain. If you want to make trigger non-enumerable so it isn't iterated over with for..in, use Object.defineProperty instead, which makes the defined property non-enumerable by default:

Object.defineProperty(Object.prototype, 'trigger', { value:  function() {
    
}});
var obj = { 4: 15, 10 : 41 }
for( item in obj ) {
    console.log(item);
}