Babel: Class inherit prototype uncorrect in IE10 using @babel/[email protected]

Created on 9 May 2018  路  3Comments  路  Source: babel/babel

Bug Report

Current Behavior

The output is undefined in IE10.

Input Code

function A() {}
A.prototype.a = 'aaa';

class B extends A {}
console.log(B.prototype.a);

Expected behavior/code

Expect output aaa, but got undefined in IE10. I also tested this in @vue/[email protected], it have the same problem.

Babel Configuration (.babelrc, package.json, cli command)

Environment

  • Babel version(s): v7.0.0-beta.36
  • Node/npm version: Node 8/npm 5
  • OS: OSX 10.13.4
  • Monorepo [e.g. yes/no/Lerna]
  • How you are using Babel: [e.g. cli, register, loader]

Possible Solution

Additional context/Screenshots

Classes outdated

Most helpful comment

use setprototypeof

import '@babel/polyfill'
Object.setPrototypeOf = require('setprototypeof')

update:

how to polyfill Object.getPrototypeOf

setprototypeof can't make getPrototypeOf helper work, but I have a workaround:

module.exports =
  Object.setPrototypeOf ||
  ({ __proto__: [] } instanceof Array ? setProtoOf : mixinProperties)

function setProtoOf(obj, proto) {
  obj.__proto__ = proto
  return obj
}

function mixinProperties(obj, proto) {
  // make getPrototypeOf helper work
  Object.defineProperty(obj, '__proto__', {
    value: proto,
  })

  for (var prop in proto) {
    if (!obj.hasOwnProperty(prop)) {
      obj[prop] = proto[prop]
    }
  }
  return obj
}

All 3 comments

Hey @sorrycc! We really appreciate you taking the time to report an issue. The collaborators
on this project attempt to help as many people as possible, but we're a limited number of volunteers,
so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack
community that typically always has someone willing to help. You can sign-up here
for an invite.

Any progress on this?

use setprototypeof

import '@babel/polyfill'
Object.setPrototypeOf = require('setprototypeof')

update:

how to polyfill Object.getPrototypeOf

setprototypeof can't make getPrototypeOf helper work, but I have a workaround:

module.exports =
  Object.setPrototypeOf ||
  ({ __proto__: [] } instanceof Array ? setProtoOf : mixinProperties)

function setProtoOf(obj, proto) {
  obj.__proto__ = proto
  return obj
}

function mixinProperties(obj, proto) {
  // make getPrototypeOf helper work
  Object.defineProperty(obj, '__proto__', {
    value: proto,
  })

  for (var prop in proto) {
    if (!obj.hasOwnProperty(prop)) {
      obj[prop] = proto[prop]
    }
  }
  return obj
}
Was this page helpful?
0 / 5 - 0 ratings