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):

    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):

    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

    Likes Received:
    Best Answers:
    Trophy Points:
    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

    Likes Received:
    Best Answers:
    Trophy Points:
    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

    Likes Received:
    Best Answers:
    Trophy Points:
    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.