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

G

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!
 
A

apatheticagnostic

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.
 
R

RobG

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).
 
G

Guest

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!
 
D

Darko

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top