OO Javascript - Class.Prototype.Property vs. This.Property

Discussion in 'Javascript' started by Spam Catcher, Nov 21, 2007.

  1. Spam Catcher

    Spam Catcher Guest

    When declaring class properties, what's the difference between:

    function EventBroker()
    {
    this.Register = function(eventName, handler)
    {

    }

    //AND

    EventBroker.prototype.PropertyName = function() {};
    }


    What's the difference between this.Property and Class.prototype.Property?

    Thanks!
    Spam Catcher, Nov 21, 2007
    #1
    1. Advertising

  2. On Nov 20, 11:50 pm, Spam Catcher <> wrote:
    > When declaring class properties, what's the difference between:
    >
    > function EventBroker()
    > {
    > this.Register = function(eventName, handler)
    > {
    >
    > }
    >
    > //AND
    >
    > EventBroker.prototype.PropertyName = function() {};
    >
    > }
    >
    > What's the difference between this.Property and Class.prototype.Property?
    >
    > Thanks!


    wouldn't this.Property be specific to an instance, while
    Class.prototype.Property would be the same (at least initially) for
    all instances? I could be mistaken, I'm rather green with javascript.
    apatheticagnostic, Nov 21, 2007
    #2
    1. Advertising

  3. Spam Catcher

    RobG Guest

    On Nov 21, 2:50 pm, Spam Catcher <> wrote:
    > When declaring class properties, what's the difference between:
    >
    > function EventBroker()
    > {
    > this.Register = function(eventName, handler)


    That will add a Register property to each and every object created
    when EventBroker is called as a constructor (in classic OO speak, each
    instance of the EventBroker class, noting that javascript does not
    have classes). The Register property of any particular instance can
    be over ridden independently of all the other instances, if you want
    to modify them all, you have to find all instances and modify each
    one.

    You can't modify the constructor dynamically, although you can replace
    the value assigned to it.


    > //AND
    >
    > EventBroker.prototype.PropertyName = function() {};


    That adds a PropertyName property to the constructor's public
    prototype property and assigns an anonymous function to it. All
    objects created using EventBroker will have access to the property.

    As with the above method, you can over-ride the property on any
    particular instance without affecting other instances, but you can
    also modify the value for all instances by replacing the value of the
    prototype's property (but not for those where the property has been
    over-ridden).


    > What's the difference between this.Property and Class.prototype.Property?



    --
    Rob
    RobG, Nov 21, 2007
    #3
  4. Spam Catcher

    Spam Catcher Guest

    RobG <> wrote in news:a8b64e12-80b9-4b41-865a-
    :

    > As with the above method, you can over-ride the property on any
    > particular instance without affecting other instances, but you can
    > also modify the value for all instances by replacing the value of the
    > prototype's property (but not for those where the property has been
    > over-ridden).


    Thank you very much! That clarified it completely!
    Spam Catcher, Nov 21, 2007
    #4
  5. Spam Catcher

    Darko Guest

    On Nov 21, 5:50 am, Spam Catcher <> wrote:
    > When declaring class properties, what's the difference between:
    >
    > function EventBroker()
    > {
    > this.Register = function(eventName, handler)
    > {
    >
    > }
    >
    > //AND
    >
    > EventBroker.prototype.PropertyName = function() {};
    >
    > }
    >
    > What's the difference between this.Property and Class.prototype.Property?
    >
    > Thanks!


    Just a note, that would be useful - the way you put it, it seems as if
    you do
    EventBroker.prototype.PropertyName = function() {};
    inside the "class" definition. That will result in reassigning the
    property
    to the function prototype each time you create a new object of the
    type. Instead
    of doing that, do something like this:

    function EventBroker()
    {
    }

    EventBroker.prototype.PropertyName = function() {};

    Cheers
    Darko, Nov 21, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. E11
    Replies:
    1
    Views:
    4,736
    Thomas Weidenfeller
    Oct 12, 2005
  2. June Lee
    Replies:
    2
    Views:
    799
    Jim Cobban
    Apr 13, 2008
  3. Replies:
    9
    Views:
    186
    Thomas 'PointedEars' Lahn
    May 26, 2006
  4. Replies:
    3
    Views:
    264
  5. javascript fish
    Replies:
    0
    Views:
    168
    javascript fish
    Oct 11, 2008
Loading...

Share This Page