.call(),.apply() and function.prototype

.call(),.apply() and function.prototype
0

#1

if .call(),.apply() are properties on function.prototype , then how comes when u log the function.prototype they are not visible…Also when u replace a function.prototype using Object.create(), the metthod .call() is not lost.
Am i missing something here?


#2

Hey there,

It looks like you’re diving deep into JavaScript’s inner workings, which is just great! One simple point of confusion, though - prototype functions aren’t actually found on the prototype property. You can see them in the __proto__ property, which makes entirely too much sense :rolling_eyes: That should answer your second question as well, since you haven’t actually changed the prototype by changing the prototype property.

Hope this helps!


#3

hey there,
thanks on the feedback…i actually managed to locate them on the .constructor then on the second__proto__
property…but here is what i don’t get if i had:

function foo(){
console.log("i am a function"+foo.name+" and "+this.name);
}


var firstObject = {
name: "i am the first object"

};
// i have overwritten the original foo.prototype--object that i hope has a .call,.bind,.apply properties and replaced it with a completely empty object
foo.prototype = Object.create(null);
foo.call(firstObject); // i am a function foo and i am the first object


how does function foo access .call() if the object it delegates to is empty


#4

You’re still looking in the wrong place. Those functions aren’t found in foo.prototype, they’re in foo.__proto__.

function foo(){
  console.log("i am a function "+foo.name+" and "+this.name);
}

var firstObject = {
  name: "i am the first object"
};

foo.__proto__ = Object.create(null); // <--------

foo.call(firstObject); 
console.log(foo);

prototype is the prototype for __proto__ and isn’t delegated to by foo. Only __proto__ is searched when making a method call.


#5

so if i am to picture foo function object,it would be something like this:

var foo = {
name: //*stuff,
length: //* more stuff,
caller: //*stuff,
displayName: //*stuff,
arguements: //*stuff,
__proto__: {
call: //*stuff,
bind: //*stuff,
apply://*stuff,
prototype:{
constructor: //*stuff,

}

}

}

#6

also by method call you mean the built in methods like bind,call etc, right? if so how does the engine differentiate this method call from the user defined ones?


#7

It doesn’t. The first method in the prototype chain to match is used. You can very easily overwrite the prototype functions of any JavaScript class and your implementation will be accessed just the same.


#8

woow…too much freedom there…anyway thanks for taking ur time to explain stuff to me…Also is my model of a function object (very sketchy) accurate?