在 JavaScript 中创建原型链 (ES-5)
我正在尝试创建一个函数来创建带有原型链的对象,如下所示:
something = object(proto1, proto2, proto3);
// Lookup order is something -> proto1 -> proto2 -> proto3 -> Object.prototype
我将包装给定的原型以覆盖它们自己的查找链(以便它们可以重用),并且希望以最少的复制来做到这一点/内部属性包装。
Javascript/ECMAScript 5 中是否有任何功能可用于覆盖对象上的所有属性访问?类似于 Python 中的 __getattribute__(self, attrname) 吗?如果不是,我该怎么办?我是否被迫克隆对象的属性(使用Object.hasOwnProperty()
)?
I'm trying to create a function that creates an object with a prototype chain, like this:
something = object(proto1, proto2, proto3);
// Lookup order is something -> proto1 -> proto2 -> proto3 -> Object.prototype
I'd to wrap the given prototypes to override their own lookup chains (so they can be reused), and want to do so with minimal copying/inner attribute wrapping.
Is there any feature in Javascript/ECMAScript 5 that can be used to override all attribute accesses on an object? Something akin to __getattribute__(self, attrname)
in Python? If not, how should I go about this? Am I forced to just clone the object's properties (with Object.hasOwnProperty()
)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JavaScript 有原型,因此可以使用如下构造:
JavaScript has prototypes, so could use a construct like: