1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Extending System Objects -- good or bad?

Discussion in 'JavaScript' started by deathshadow, Feb 6, 2020.

  1. #1
    I'm asking this across several forums as I'm looking for as many eyeballs on target with this.

    I remember about a decade ago people telling me that doing something like:

    String.prototype.__condense = function() {
    Code (markup):
    or

    Object.addProperty(String.prototype, '__htmlSpecialChars', {
    Code (markup):
    Was bad. I can only remember two of the reasons:

    1) Possibility of namespace collision with future actual ECMAScript object methods.

    2) Doesn't inherit properly in IE7/earlier on Node or descendants of Node such as Element

    As I don't care anymore about IE9/earlier (degrade to scripting off behavior since I actually have graceful degradation) and avoid official namespace issues by prefixing a double underscore... but I could have sworn there were more reasons not to do this.

    I just can't remember what those reasons were and my Google-fu is failing me miserably.

    It just feels like being able to say:

    var escaped = myString.__htmlSpecialChars;
    Code (markup):
    is more convenient/sensible than REALLY polluting the global namespace with:

    function htmlSpecialChars(str) {
    Code (markup):
    and...

    var escaped = htmlSpecialChars(myString);
    Code (markup):
    Since it removes the value passage from it, as 'this' is hard-coded to the handler, removing extra stack manipulation.

    So... can anyone list out other problems with extending / morphing the browser system objects like String, Document, Node, Element, etc, etc?
     
    deathshadow, Feb 6, 2020 IP
  2. scu8a

    scu8a Greenhorn

    Messages:
    81
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    15
    #2
    I'm just guessing, but if I wanted easy access to my custom objects while writing code with auto-suggestion, anything that leads with an underscore would bring up a list of my custom objects at the top (provided we assume it's ordered alphabetically), hence speeding up the development time. That's generally the only reason I would have for placing underscore character(s) into object names. I use the convention sometimes, but w/ 2 underscores? That's like wearing a pocket protector for your pocket protector. Not that there's anything wrong with that.
     
    scu8a, Feb 10, 2020 IP
  3. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #3
    The what now? Oh auto suggest, more stupid pointless BS that just gets in my damned way for being distracting. aka the type of crap that makes me not use IDE's or any editor where I can't turn that BS off.

    Like the illegible idiotic acid trip that is colour syntax highlighting, or the painfully useless autocomplete and tag completion bull.
     
    deathshadow, Feb 11, 2020 IP
  4. HarryPAO

    HarryPAO Peon

    Messages:
    12
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    3
    #4
    Actually that's the main reason. And the case that a third party plugin may extend the same property so it may overwrite what you did.

    Also, there had been some reported cases that Chromium did not like that but I think that nowdays that's quite rare (can't find the bug tracking code at the moment).
    Generally speaking feel free to extend to your heart's joy
    (funny note: in the past facebook used this technique to disable console.log , https://stackoverflow.com/questions/21692646/how-does-facebook-disable-the-browsers-integrated-developer-tools)
     
    HarryPAO, Feb 24, 2020 IP
    deathshadow likes this.