Skip to content

Cannot read property 'prototype' of undefined #10837

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
nin-jin opened this issue Sep 10, 2016 · 1 comment
Closed

Cannot read property 'prototype' of undefined #10837

nin-jin opened this issue Sep 10, 2016 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@nin-jin
Copy link

nin-jin commented Sep 10, 2016

script.ts

class A extends B {}
class B { static foo() {} }

Produces broken js that throws subject exception

var __extends = (this && this.__extends) || function (d, b) {
    for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var A = (function (_super) {
    __extends(A, _super);
    function A() {
        _super.apply(this, arguments);
    }
    return A;
}(B));
var B = (function () {
    function B() {
    }
    B.foo = function () { };
    return B;
}());

We need to produce js like this

var __extends = (this && this.__extends) || function (d, b) {
    if( Object.setPrototypeOf ) Object.setPrototypeOf( d , b )
    else for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
    function __() { this.constructor = d; }
    d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
function A() {
    B.apply(this);
}
__extends(A, B);
function B() {
}
B.foo = function () { };
  1. This is short and clear code.
  2. This code are close to code writed by human.
  3. This works right in modern browsers.
@kitsonk
Copy link
Contributor

kitsonk commented Sep 10, 2016

Classes are not hoisted (just like let and const), as per the ES2015 spec and so your code does not work in modern browsers. In Chrome it produces Uncaught ReferenceError: B is not defined.

Currently TypeScript does not warn you of this, but this is covered by #5207 and planned for TypeScript 2.1.

@nin-jin nin-jin closed this as completed Sep 10, 2016
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 10, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants