question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Custom array class methods disappear

See original GitHub issue

Current behavior

Custom array class methods disappear:

class MyArray extends Array<number> {
  public toString(): string {
    return 'Custom toString method called!';
  }

  public toDB(): string[] {
    return Array.from(this.map((t) => t.toString()));
  }
  
  public static fromDB(record: string[]): MyArray {
    return new MyArray(...record.map((t) => Number(t)));
  }
}

 // Logs an empty array `[]`
console.log('MyArray:', new MyArray());
// Logs an empty string (calls Array#toString instead of MyArray#toString)
console.log(`MyArray: ${new MyArray().toString()}`); 
// Logs `undefined` (even though I defined a MyArray#toDB method)
console.log('MyArray toDB:', new MyArray().toDB);
// Logs `[Function (anonymous)]` (so static methods are preserved)
console.log('MyArray fromDB:', MyArray.fromDB);

Desired behavior

Cypress should compile that custom MyArray class and it’s methods properly.

// Should log `Custom toString method called!` instead of an empty string
console.log(`MyArray: ${new MyArray().toString()}`); 
// Should log `[Function (anonymous)]` instead of `undefined`
console.log('MyArray toDB:', new MyArray().toDB);

Test code to reproduce

See description above.

Cypress Version

Cypress package version: 8.1.0 Cypress binary version: 8.1.0 Electron version: 12.0.0-beta.14 Bundled Node version: 14.15.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:7

github_iconTop GitHub Comments

3reactions
sainthkhcommented, Aug 31, 2021

@nicholaschiang Did it solve the problem? If so, we can close the issue.

1reaction
sainthkhcommented, Sep 21, 2021

Closing because of the inactivity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JS prototype method disappears when object is sent to client ...
The usual work-around for a case like yours is to just send an array of plain values. The receiving end knows it will...
Read more >
Implementation of Array class in JavaScript - GeeksforGeeks
This article implements Arrays using JavaScript. An Array is a simple data Structure, in which elements are stored in contiguous memory ...
Read more >
Classes - JavaScript - MDN Web Docs
Classes are a template for creating objects. They encapsulate data with code to work on that data. Classes in JS are built on...
Read more >
Arrays - Julia Documentation
Construct an N -dimensional Array containing elements of type T , initialized with missing entries. Element type T must be able to hold...
Read more >
pandas.api.extensions.ExtensionArray
pandas will recognize instances of this class as proper arrays with a custom type and will not attempt to coerce them to objects....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found