0

I've written a class trying to extend the native Javascript Array class with a custom class, let's call it MyClass. This is basically what it looks like:

class MyClass extends Array

  constructor: (obj) -> @push.apply @, obj

  first: -> @slice 0, 1

Instantiating the class is no problem. Running this in the console:

var myInstance = new MyClass(["1", "2"])
> ["1", "2"]
myInstance instanceof MyClass
> true
myInstance instanceof Array
> true

works as exptected.

The problem is that if I run:

myInstance.first()
> ["1"] // as expected
myInstance.first() instanceof MyClass
> false // not expected
myInstance.first() instanceof Array
> true

the returned value is no longer an instance of MyClass.

I've also tried @__proto__.first = @first in the constructor function and first: -> @slice.call @, 0, 1. But with no success.

Why doesn't myInstance.first() instanceof MyClass return true?

2
  • 1
    I don't think extending Array will ever work properly (stackoverflow.com/q/3261587/479863), why are you trying to do it? Commented Mar 18, 2014 at 18:57
  • Basically this is only experimental, I wanted to see if it's possible. Instead of polluting the Array class, I wanted to rather augment my own class. Commented Mar 19, 2014 at 9:47

1 Answer 1

1

Why doesn't myInstance.first() instanceof MyClass return true?

Because first calls slice, and Array.prototype.slice does always return an Array. You will need to overwrite it with a method that wraps it in a MyClass again:

class MyClass extends Array

  constructor: (obj) -> @push.apply @, obj

  slice: () -> new MyClass super
  splice: () -> new MyClass super
  concat: () -> new MyClass super
  filter: () -> new MyClass super
  map: () -> new MyClass super

  first: -> @slice 0, 1

And notice that subclassing Array does not work.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! That solves the issue! However regarding memory footprint, wouldn't new MyClass super consume a bit more memory? Also, thanks for the article link!
Sure it will. However, you want more than an Array :-)
Yep, I totally get that. My questions should have been phrased like this; is there a way to create a new instance once, instead of each time I use for example slice()?
I don't understand, the purpose of slice is to return a different, new instance? Do you want it to mutate your instance?
No, it doesn't modify. All the other array methods that do mutate (like sort, push, shift, …) work on MyClass instances without problems.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.