In this example it seems that a callback is accessing another parameter (without having to provide the argument again).
Excerpt from the link above
var SimplePropertyRetriever = {
getPrototypeEnumerables: function(obj) {
return this._getPropertyNames(obj, false, true, this._enumerable);
},
_enumerable: function(obj, prop) {
return obj.propertyIsEnumerable(prop);
},
_getPropertyNames: function getAllPropertyNames(obj, iterateSelfBool, iteratePrototypeBool, includePropCb) {
...
}
}
As seen:
this._enumerableis passed to_getPropertyNames_enumerableaccepts a parameter calledobjobjis not explicitly passed down though, so it seems that when the callback is passed to_getPropertyNamesit somehow accesses its first argument, which isobj
To test it, I tried the below, which did not work.
function myFunc2(para, callback) {
console.log(`Para: ${para}`);
callback();
}
myFunc2(42, (para) => console.log(para));
Any idea what I am missing here?
para- "console.log(Para: ${para}, callback: ${callback(para)});"callback();. Thisparais not the same than in your callback defined in your call, you should distinct them to avoid confusion:myFunc2(42, (x) => console.log(x));this._enumerabledoes not passobjdown. To my understanding, I would have written it asthis.enumerable(obj, prop)_enumerableaccessesobjin_getPropertyNames, whenobjis never passed to it.return this._getPropertyNames(obj, false, true, this._enumerable);doesn't call at all thethis._enumerablefunction. It gives it as param tothis._getPropertyNamesso thatincludePropCbis now this function. It has yet to be called inthis._getPropertyNamesunder that name (if needed).this._getPropertyNamesalready hasobjas a parameter, so he can use it with the function if we want to.