how define the type of variable in constractor javascript oo
Class Personne{
constractor(name){
this.name = name
}
}
how can i make the name type string
For further actions, you may consider blocking this person and/or reporting abuse
how define the type of variable in constractor javascript oo
Class Personne{
constractor(name){
this.name = name
}
}
how can i make the name type string
For further actions, you may consider blocking this person and/or reporting abuse
Philip John Basile -
Diona Rodrigues -
Pragyan Tripathi -
Jeongho Nam -
Once suspended, ayoubelsvg will not be able to comment or publish posts until their suspension is removed.
Once unsuspended, ayoubelsvg will be able to comment and publish posts again.
Once unpublished, all posts by ayoubelsvg will become hidden and only accessible to themselves.
If ayoubelsvg is not suspended, they can still re-publish their posts from their dashboard.
Once unpublished, this post will become invisible to the public and only accessible to Ayoub.
They can still re-publish the post if they are not suspended.
Thanks for keeping DEV Community safe. Here is what you can do to flag ayoubelsvg:
Unflagging ayoubelsvg will restore default visibility to their posts.
Top comments (9)
well if you really want the constructor to only receive string, you can just throw an error if the parameter is not a string with "typeof"
if(typeof name !== 'string') throw Error('Only string is allowed you mother fucker');
but nowadays devs use Typescript instead, they can just config the compiler to fail the build if it's violate TS rules, like passing number into string
thanks
I guess I'm not a dev then. Hmm?
One option is to switch to TypeScript because, as others have stated, JS does not support static typing but TS can help.
A lower impact approach if you are using VSCode is to use type annotations.
However, I will not stop this sort of thing happening.
But you could change the constructor to state,
You don't. JS doesn't allow you to define the types of variables
This is not a problem, it's just a different way of doing things
thanks
Javascript has dynamic typing system. So the interpreter will trye to assign a type in execution time.
If you really want to use a static typing you should try to use typescript.
Try this
this.name= String(name)