0

Is it in someway possible to reuse a class name that is defined in the Global objects list found here? (Like Number, String)

Lets say that I want to have my own String class. I can define it, and use it like this:

String.js

export default class String {
}

App.js

import String from './String'

let string = new String();

This actually works, but then

  • PHPStorm tells me: yeah you used a primitive object wrapper (thinking it's the global object String).
  • Then ESLint tells me: Disallow Primitive Wrapper Instances (no-new-wrappers)
  • And lastly SonarQube tells me: The use of wrapper objects for primitive types is gratuitous, confusing and dangerous. Simple literals should be used instead.

So yeah is there a way to encapsulate my class so it doesn't get confused with the global String class?

12
  • 4
    Doing this is extremely bad practice. Why would you want to do that? Commented Oct 1, 2017 at 9:53
  • 1
    This sums up my reaction. Commented Oct 1, 2017 at 9:55
  • Because the object I'm trying to blueprint with my class is an actual string. Like the cloth. I've looked for synonyms, but there simply are none. Commented Oct 1, 2017 at 9:55
  • 1
    @DavidMaes I don't see the problem. Although everyone is telling you that it's not a good idea, your code still runs, doesn't it? So it's perfectly possible in JS (in fact, I can't even get ESLint to complain about it). Commented Oct 1, 2017 at 10:05
  • 1
    @DavidMaes it's bad practise because it makes code harder to understand for others. If that is not of your concern, you can create classes called String and hopefully never run into the situation where you also need to use the global constructor (although you can always set up something like instruments.String instead). It's not very helpful to keep referring to other languages when clearly it is possible what you want. Commented Oct 1, 2017 at 10:19

1 Answer 1

1
import './String'

Function and class definitions are local to the module. You're importing the file, but not using anything from it.

That's why this:

let string = new String();

will use the global String—there is no String definition in the module scope.

Make sure you name the imports you need:

import String from './String'

Incidentally, this is one of multiple reasons why it is better practice to give your String class a unique name, like PhysicsString or StringMaterial or Thread. That way if you forget to import it somewhere, you won't accidentally be using the global definition.

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

9 Comments

Ah That was actually my mistake, I actually already do what you said. I've editted the question. Same problem though. Like I said, the JS does the trick. But my code conventions don't see that I'm using my own class instead.
@DavidMaes Most likely the linters go by the name of the constructor only, and don't actually look it up in the scope. I would however expect some kind of "`import is shadowing global variable" warning as well.
@Bergi I know that all my classes are encapsulated very clearly, and therefore shadowing is something that can't be done "by accident". This is not bad practice if the class you're using obviously imports a new definition for String.
Oohh, I see, it's just the linters. Sorry, I missed that. For eslint you could disable the rule, heh, but it's probably easier to rename the class…
Then I'm afraid the only remaining solution that I can think of is to patch the linters with scope analysis ://
|

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.