Skip to content

In JS, function declarations should allow subsequent prototype assignment #23007

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
sandersn opened this issue Mar 29, 2018 · 1 comment · Fixed by #32944
Closed

In JS, function declarations should allow subsequent prototype assignment #23007

sandersn opened this issue Mar 29, 2018 · 1 comment · Fixed by #32944
Assignees
Labels
Bug A bug in TypeScript Domain: JavaScript The issue relates to JavaScript specifically
Milestone

Comments

@sandersn
Copy link
Member

// @allowJs: true
// @checkJs: true
// @Filename: a.js
function C(p) {
  this.p = p
}
C.prototype = {
  m() {
    console.log(this.p)
  }
}
C.prototype.q = function(r) {
  return this.p === r
}
var c = new C(1)
c.q(2)
c.m()

Expected behavior:
Both q and m are methods on c.

Actual behavior:
Only q is a method on c; m is not recognised.

Note that if the declaration is var C = function(p) { this.p = p }, everything works. This only happens when C is a function declaration instead of a variable declaration with a function initialiser.

@mhegazy mhegazy added Bug A bug in TypeScript Salsa labels Mar 30, 2018
@mhegazy mhegazy added this to the TypeScript 2.9 milestone Mar 30, 2018
@sandersn
Copy link
Member Author

Another repro from axios:

function E() {
  this.message = 'String contains an invalid character';
}
E.prototype = new Error;
E.prototype.code = 5;
E.prototype.name = 'InvalidCharacterError';

@mhegazy mhegazy modified the milestones: TypeScript 3.0, Future Jul 2, 2018
@weswigham weswigham added Domain: JavaScript The issue relates to JavaScript specifically and removed Salsa labels Nov 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: JavaScript The issue relates to JavaScript specifically
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants