Skip to content
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

Extending Array does not work correctly #571

Closed
pjako opened this issue Jan 22, 2015 · 6 comments
Closed

Extending Array does not work correctly #571

pjako opened this issue Jan 22, 2015 · 6 comments
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue

Comments

@pjako
Copy link

pjako commented Jan 22, 2015

Hi, I just tried to extend Array in your playground, and tested it then again in an traceur playground

class FooArray extends Array {
  constructor(a) {
    super(a);
  }
}
var e = new FooArray();
e.push('bar');
console.log(e);

6to5: {"0":"bar","length":1}

tracur: ["bar"]

@zloirock
Copy link
Member

Try in traceur repl console.log(Array.isArray(e)) and open console in 6to5 repl. See caveats.

@stefanpenner
Copy link
Member

I don't believe Natives can be subclassed yet, unfortunately this is a platform limitation.

In theory some hacks could exist to pseudo implement 1 level of inheritance by fetching the native from another realm and mutating it, but this really is a slippery slope.

@pjako
Copy link
Author

pjako commented Jan 22, 2015

class FooArray extends Array {
  constructor(a) {
    super(a);
  }
}
var e = new FooArray();
e.push('bar');
console.log(e);
console.log(e.length);
console.log(e instanceof Array);
console.log(Array.isArray(e))

traceur:
["bar"]
1
true
false
6to5:
{"0":"bar","length":1}
1
true
false

@fkling
Copy link
Contributor

fkling commented Jan 22, 2015

Duplicate? #570

@jamiebuilds
Copy link
Contributor

See Caveats

@chocolateboy
Copy link
Contributor

Leaving this here for anyone Googling this error message:

subclass_string.js

class MyString extends String {
    constructor (value) {
        super(value)
    }
}

let test = new MyString('foo');

test += 'bar';
console.log(test);

result

$ babel-node subclass_string.js

subclass_string.js:9
test += 'bar';
    ^
TypeError: String.prototype.valueOf is not generic

@lock lock bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jan 18, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Jan 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue
Projects
None yet
Development

No branches or pull requests

6 participants