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

Support Array subclassing #9259

Open
amrdoe opened this issue Dec 27, 2018 · 4 comments
Open

Support Array subclassing #9259

amrdoe opened this issue Dec 27, 2018 · 4 comments

Comments

@amrdoe
Copy link

amrdoe commented Dec 27, 2018

Version

2.5.21

Reproduction link

https://codepen.io/AmrIKhudair/pen/NevxML

Steps to reproduce

1- create a class extending Array and add a custom method
2- instantiate a new instance of the class and add it to data
3- try to call the method on the instance in the template

What is expected?

The method is successfully executed

What is actually happening?

Error not a function

@yyx990803
Copy link
Member

Vue 2 currently does not support Array subclassing (Vue 3 development branch already does). I will convert this into a feature request.

@yyx990803 yyx990803 changed the title Classes extending Array are converted into Array again Support Array subclassing Dec 27, 2018
@edcoreweb
Copy link

@AmrIKhudair
I needed this as well and made it work in user-land. When Vue 3 arrives you can remove it.

export function makeReactive (obj) {
  // eslint-disable-next-line no-proto
  const proto = obj.__proto__

  Object.defineProperty(obj, '__proto__', {
    get () { return proto },
    // eslint-disable-next-line no-proto
    set (newValue) { proto.__proto__ = newValue }
  })
}
import { makeReactive } from 'util'

export default class UserCollection extends Array {
  constructor (...args) {
    super(...args)

    makeReactive(this)
  }
}

@amrdoe
Copy link
Author

amrdoe commented Feb 22, 2019

@edcoreweb
Thanks a lot

@lackneets
Copy link

@AmrIKhudair I needed this as well and made it work in user-land. When Vue 3 arrives you can remove it.

export function makeReactive (obj) {
  // eslint-disable-next-line no-proto
  const proto = obj.__proto__

  Object.defineProperty(obj, '__proto__', {
    get () { return proto },
    // eslint-disable-next-line no-proto
    set (newValue) { proto.__proto__ = newValue }
  })
}

You saved my day in 2022

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants