By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

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

P: n/a

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!
Nov 21 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Nov 20, 11:50 pm, Spam Catcher <spamhoney...@rogers.comwrote:
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.
Nov 21 '07 #2

P: n/a
On Nov 21, 2:50 pm, Spam Catcher <spamhoney...@rogers.comwrote:
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
Nov 21 '07 #3

P: n/a
RobG <rg***@iinet.net.auwrote in news:a8b64e12-80b9-4b41-865a-
3c**********@v4g2000hsf.googlegroups.com:
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!
Nov 21 '07 #4

P: n/a
On Nov 21, 5:50 am, Spam Catcher <spamhoney...@rogers.comwrote:
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
Nov 21 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.