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.

Do not use constructor.name to check object

See original GitHub issue

isAbortSignal and isBlob use constructor.name only to check.

https://github.com/bitinn/node-fetch/blob/bf8b4e8db350ec76dbb9236620f774fcc21b8c12/src/request.js#L36-L43

https://github.com/bitinn/node-fetch/blob/95286f52bb866283bc69521a04efe1de37b26a33/src/body.js#L364-L378

However, constructor.name can be rewrited. So, for example, webpack4 with production mode will break constructor.name of AbortSignal that will be rewritten to "s".

image

So, Expected signal to be an instanceof AbortSignal Error was occured (ref: https://github.com/yumetodo/vscode-google-photos-uploader/issues/11 )

isURLSearchParams is adopting good implementation (ref: https://github.com/bitinn/node-fetch/issues/296#issuecomment-307598143, https://github.com/bitinn/node-fetch/pull/297/commits/302992d68e75748a7781c52e868b4fde7925325f ). Object.prototype.toString() will call Symbol.toStringTag propaty which is not writable(see MDN)

https://github.com/bitinn/node-fetch/blob/95286f52bb866283bc69521a04efe1de37b26a33/src/body.js#L346-L362

Of course, I know that there are two workarounds for webpack4:

  1. Set optimization.minimize to false (ref: https://github.com/webpack/webpack/issues/6841#issuecomment-376129163)
  2. Specify minimizer manually and set keep_fnames to true (ref: https://github.com/CartoDB/carto-vl/pull/1066)

However, rely on constructor.name is a bad idea, I think.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Richienbcommented, Sep 17, 2019

Fixed in #673

3reactions
bitinncommented, Sep 8, 2019

OK, we will drop constructor.name in favor of toStringTag in v3 release where applicable.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can i use constructor.name to detect types in JavaScript
Don't use obj.constructor because it be changed, although you might be able to use instanceof to see if it is correct:
Read more >
Object.prototype.constructor - JavaScript - MDN Web Docs
The constructor data property of an Object instance returns a reference to the constructor function that created the instance object.
Read more >
constructor.name - Beyond Java
constructor.name to the variable name, and you'll see the name of the class of the object. Awesome! Avoid using .constructor.name in your ...
Read more >
JavaScript Object Constructors - W3Schools
It is considered good practice to name constructor functions with an upper-case first letter. About this. In a constructor function this does not...
Read more >
How to get the Class Name of an Object in JavaScript
While creating an object of the class, the corresponding constructor is called either explicitly or implicitly. If no constructor is defined, ...
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