我在React-native上工作,在很多地方都展示了很多代码(例如https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/super

let Vehicle = {
    debug() {
        return 'Vehicle';
    }
};

let Car = {
    debug() {
        return super.debug() + ' is a Car';
    }
};


无论我如何实例化Car,我都会收到'super' outside of function or class错误:

//set prototype on object
Object.setPrototypeOf(Car, Vehicle);
Car.debug(); //error
//create instance
let c = Object.create(Car);
//set prototype on instance
Object.setPrototypeOf(c, Vehicle);
c.debug(); //error


为什么会引发此错误?如何使用对象调用super

最佳答案

好像setPrototypeOf在本机https://facebook.github.io/react-native/docs/javascript-environment.html中不兼容。在这里,我没有看到Object.setPrototypeOf https://github.com/facebook/react-native/blob/master/babel-preset/configs/main.js#L16的babel插件。

关于javascript - 函数或类之外的“ super ”-react-native,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41923501/

10-13 07:35