Adding properties/methods to a defined property?

Discussion in 'JavaScript' started by deathshadow, Sep 28, 2019.

  1. #1
    If I have a property created with Object.defineProperty where I want the getter to return a string, is it possible to attach methods to that string without screwing with the global String.prototype and without accidentally converting it to a generic object?

    I know that sounds kind of wonky... think of it like how Element.classList works. Where Element.classList returns and array, but that array has extra properties like add/remove/toggle.

    Or even just Number where it behaves as a number variable, but also has methods.

    I can't seem to create that with anything that behaves like a string.
     
    deathshadow, Sep 28, 2019 IP
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #2
    Nevermind, got it. As I don't need it always applied I can do so on the fly during creation, I just had to be sure to use a "new String(myString)" to make sure as to not pollute the global String object.
     
    deathshadow, Sep 28, 2019 IP