我可以使用ES2015课程扩展代理吗? [英] Can I extend Proxy with an ES2015 class?

查看:111
本文介绍了我可以使用ES2015课程扩展代理吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试扩展代理,如下所示:

I tried to extend Proxy, like so:

class ObservableObject extends Proxy {}

我使用Babel将其转换为ES5,我在浏览器中收到此错误:

I used Babel to transpile it to ES5, and I got this error in the browser:

app.js:15 Uncaught TypeError: Object prototype may only be an Object or null: undefined

我查看了它指向的代码行。这是代码的一部分,箭头指向有问题的代码行:

I looked at the line of code it pointed to. Here's that portion of the code with arrows pointing to the offending line of code:

var ObservableObject = exports.ObservableObject = function (_Proxy) {
    _inherits(ObservableObject, _Proxy); // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

    function ObservableObject() {
        _classCallCheck(this, ObservableObject);

        return _possibleConstructorReturn(this, Object.getPrototypeOf(ObservableObject).apply(this, arguments));
    }

    return ObservableObject;
}(Proxy);

有谁知道为什么我会收到此错误?这是巴贝尔的错误吗?当您尝试扩展代理时会发生什么?

Does anyone know why I might be getting this error? Is this a bug in Babel? What is supposed to happen when you try to extend Proxy?

推荐答案

不,ES2015类无法扩展代理 1

No, an ES2015 class cannot extend Proxy1.

代理对象具有非常不典型的语义,在ES2015中被视为异国情调对象,这意味着他们没有一个或多个必须由所有对象支持的基本内部方法的默认行为。它们没有任何原型,这是您通常可以获得所扩展类型的大部分行为的地方。来自第26.2.2节:属性规范构造函数在规范中

Proxy objects have very atypical semantics and are considered "exotic objects" in ES2015, meaning that they do "not have the default behaviour for one or more of the essential internal methods that must be supported by all objects". They do not have any prototype, which is where you'd normally get most of the behaviour for a type you're extending. From section 26.2.2: "Properties of the Proxy Constructor" in the specification:


代理构造函数没有 prototype 属性,因为代理异常对象没有需要初始化的[[Prototype]]内部插槽。

The Proxy constructor does not have a prototype property because proxy exotic objects do not have a [[Prototype]] internal slot that requires initialization.

这不是巴别塔的限制。如果您尝试在Chrome中扩展代理,它本身支持它和类语法,您仍会收到类似的错误:

This is not a limitation of Babel. If you attempt to extend Proxy in Chrome, where it and the class syntax are both natively supported, you'll still get a similar error:


未捕获的TypeError:类扩展值没有有效的原型属性undefined

Uncaught TypeError: Class extends value does not have valid prototype property undefined

1 否是实际答案。但是, Alexander O'Mara 指出,如果您为 Proxy.prototype分配值(毛!),至少在某些浏览器中,它确实可以扩展。 我们对此进行了一些实验。由于外来代理实例的行为,这不能用于完成包装构造函数的功能,并且某些行为在浏览器之间似乎不一致(我不确定规范期望什么如果你这样做)。请不要在严肃的代码中尝试这样的事情。

1 "No" is the practical answer. However, Alexander O'Mara pointed out that if you assign a value to Proxy.prototype (gross!), it does become possible to extend, at least in some browsers. We experimented with this a little. Due to the behaviour of exotic Proxy instances this can't be used to accomplish much more than you could do with a function wrapping the constructor, and some behaviour does not appear to be consistent between browsers (I'm not sure what the specification expects if you do this). Please don't attempt anything like this in serious code.

这篇关于我可以使用ES2015课程扩展代理吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆