2

I'm creating a javascriptt library, and I need to add a function to the prototype of the String class, (isEmpty function), I can't really understand whether this will be global in all the project. Let's say the user of the library added a function with the same name to the String prototype, will that override the one in the library?

challenger
  • 2,184
  • 1
  • 18
  • 25
  • 3
    **Yes** for the first and **Yes** for the second! – ibrahim mahrir Mar 13 '17 at 19:20
  • 6
    This is why you should not add a function to the prototype of built-in objects unless you absolutely know what you're doing. Instead, define a function in your own library, like `isEmpty(s)`, where `s` is a string. See http://stackoverflow.com/q/14034180/215552 for more info. – Heretic Monkey Mar 13 '17 at 19:23
  • @ibrahimmahrir thanks, is there any thing we do about that ? like to avoid conflict ? – challenger Mar 13 '17 at 19:23
  • 2
    Do as @MikeMcCaughan said! It's not a good practice to alter those built-in objects. And if the library uses them (functions) internally then no need to add them to the prototype at all. – ibrahim mahrir Mar 13 '17 at 19:25

0 Answers0