@babel/plugin-transform-object-super

object-super - 图1info

This plugin is included in @babel/preset-env

Examples

In

JavaScript

  1. let obj = {
  2. say() {
  3. return "Hello";
  4. },
  5. };
  6. let obj2 = {
  7. say() {
  8. return super.say() + "World!";
  9. },
  10. };

Out

JavaScript

  1. var _obj;
  2. var _get = function get(object, property, receiver) {
  3. if (object === null) object = Function.prototype;
  4. var desc = Object.getOwnPropertyDescriptor(object, property);
  5. if (desc === undefined) {
  6. var parent = Object.getPrototypeOf(object);
  7. if (parent === null) {
  8. return undefined;
  9. } else {
  10. return get(parent, property, receiver);
  11. }
  12. } else if ("value" in desc) {
  13. return desc.value;
  14. } else {
  15. var getter = desc.get;
  16. if (getter === undefined) {
  17. return undefined;
  18. }
  19. return getter.call(receiver);
  20. }
  21. };
  22. var obj = {
  23. say: function say() {
  24. return "Hello";
  25. },
  26. };
  27. var obj2 = (_obj = {
  28. say: function say() {
  29. return (
  30. _get(_obj.__proto__ || Object.getPrototypeOf(_obj), "say", this).call(
  31. this
  32. ) + "World!"
  33. );
  34. },
  35. });

Installation

  • npm
  • Yarn
  • pnpm
  1. npm install --save-dev @babel/plugin-transform-object-super
  1. yarn add --dev @babel/plugin-transform-object-super
  1. pnpm add --save-dev @babel/plugin-transform-object-super

Usage

babel.config.json

  1. {
  2. "plugins": ["@babel/plugin-transform-object-super"]
  3. }

Via CLI

Shell

  1. babel --plugins @babel/plugin-transform-object-super script.js

Via Node API

JavaScript

  1. require("@babel/core").transformSync("code", {
  2. plugins: ["@babel/plugin-transform-object-super"],
  3. });