I am just curious whether I can include an object into function prototype chain. What I mean:
var test = function() { return 'a'; };
console.log(test.bind(this)); // return new bound function
// changing the prototype
// in console the prototype is really set as needed
// test => new object => function prototype => Object prototype
var F = function() {};
F.prototype = Function.prototype;
test.prototype = new F();
console.log(test.bind()); // this still works and returns new bound function
test.prototype.bind = function() {
return 'test';
};
console.log(test.bind()); // this does not work as expected -> returns new bound function instead of 'test'. When I do delete Function.prototype.bind, then console.log(test.bind) returns undefined
You have a function
test. It is ainstanceof Function, and inherits fromFunction.prototypeso that you can calltest.bindfor example.Then you set the “prototype” property of your function to an object inheriting from
Function.prototype. That means that all instances oftestwill inherit from that object (and from Function.prototype):Then you overwrite the test property on your custom prototype object. You do good to do so, because Function methods should only be called on functions (i.e. callable objects):
In your tests with
console.logyou only apply the Function methods on yourtestfunction, not on instances of it. Good, because they’d fail. So, I can see no reason why any objects should inherit fromFunction– you can’t construct functions that don’t inherit directly fromFunction.