函数的自定义原型链
Custom prototype chain for a function
我只是好奇是否可以将对象包含在函数原型链中。我的意思是:
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
.它是一个instanceof Function
,并且继承自Function.prototype
,因此您可以调用test.bind
例如。
然后,将函数的"prototype"属性设置为从Function.prototype
继承的对象。这意味着test
的所有实例都将继承自该对象(以及 Function.prototype):
var t = new test;
t instanceof test; // => true
t instanceof Function; // => true
然后覆盖自定义原型对象上的测试属性。这样做很好,因为函数方法应该只在函数(即可调用对象)上调用:
>>> Function.prototype.bind.call({})
Unhandled Error: Function.prototype.bind: this object not callable
在使用console.log
的测试中,仅对test
函数应用函数方法,而不对其实例应用 Function。很好,因为他们会失败。所以,我看不出任何对象应该从Function
继承的理由 - 你不能构造不直接从Function
继承的函数。
相关文章:
- 如何将原型用于自定义方法和对象操作
- 使用原型对象向javascript对象添加自定义属性和方法的建议
- 使用自定义原型实例化JavaScript函数
- Javascript 自定义数组原型
- 在 JavaScript 中的自定义对象中使用原型
- 函数的自定义原型链
- 使用原型的自定义数组函数.新的 MyArray(1,2,3,4) 不工作
- 未在自定义 OL3 控件的构造函数中定义的原型属性
- document.registerElement 扩展了 HTMLInputElement 原型,同时使用自定义标记名称
- 如何用自定义函数替换JavaScript原型
- 不将函数声明为另一个自定义函数的原型的用例
- 自定义方法以及web-dev中构造函数和原型的有用性
- 检查自定义元素原型是否源自特定原型
- 使用HTMLElement作为自定义函数原型时的奇怪属性行为
- 自定义firefoxconsole.log(用于我自己的原型)
- 谷歌地图API -自定义原型标记图像从今天开始不显示
- 将自定义对象传递给webworker会导致丢失原型信息
- 你能在JavaScript中使用自定义原型创建函数吗?
- 使用encodeuriccomponent()的字符串原型自定义方法
- 原型自定义事件不在DOM元素上