我只是好奇我是否可以將一個對象包含到函數原型鏈中。我的意思是:函數的自定義原型鏈
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
'test.prototype.bind'增加了一個方法來test',即'新的測試()。bind'的'實例。它沒有定義'test.bind'。在第一種情況下,'test.bind'直接從'Function.prototype'繼承,而不是'test.prototype'。 –
就我所見,不可能在不修改'Function.prototype'的情況下將自定義方法添加到函數原型鏈中? –