Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Object.prototype.method or Object.method

  • This topic is empty.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #195431
    shaneisme
    Participant

    So I’ve been reading up on Object.prototype, but I don’t know what the difference is between the following snippets:

    Object = {};
    
    Object.prototype.foo = function() {
      return 'Hello world';
    }
    

    And:

    Object = {};
    
    Object.foo = function() {
      return 'Hello world';
    }
    

    Both seem to do the exact same thing to me, but from what I’ve read using prototype is more performant in system memory since only one copy is made? Perhaps I’ve missed something.

    Are there good reasons to use either/or?

    #195490
    shaneisme
    Participant

    First, yes your assumption is correct.

    And second – thanks for laying it all out for me that makes sense.

    So would using prototype by similar to having a “public” function/variable/etc. vs. “private”?

    #195496
    shaneisme
    Participant

    Ah ok, I got it now – appreciate it.

Viewing 3 posts - 1 through 3 (of 3 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.