简体   繁体   中英

ES6 push to static Array property

I have an ES6 class defined as such

const myMixin = (SomeOtherClass) => {
  class Something extends SomeOtherClass {
    static get __someProperty__() {
      return SomeOtherClass.__someProperty__ ? SomeOtherClass.__someProperty__.slice(0) : [ SomeOtherClass.name ];
    }
  }
...

Then, if I try to push a value into that array and then log its value, it remains the same.

Something.__someProperty__.push('someValue');
console.log(Something.__someProperty__);
//output: ['SomeOtherClass'], instead of ['SomeOtherClass', 'someValue']

I would have assumed that since the value of the property __someProperty__ is an Array assigned at declaration, accessing it would always be a reference to the same structure and that there wouldn't be a problem pushing to it. Am I missing something or getting something wrong?

Thanks!

it would always be a reference to the same structure

No you changed that. Something.__someProperty__ evaluates to a new cloned array due to the .slice(0) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM