473,396 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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


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
4 2168
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Vincent van Beveren | last post by:
Hi everyone, I have the following code using inheritence The important part is the function getName(). The result should alert 'John Doe': function First(name) { this.name = name; ...
5
by: Greg Swindle | last post by:
Hello, I have a question about how prototyping relates to variables and their scope. Given the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var ParentObject = function() {...
12
by: shlomi.schwartz | last post by:
Q: is it possible to inherit from a native object? I tried to use Date object as the base object like so: x.prototype = Date.prototype; x.prototype.constructor = x; function x(){...
3
by: petermichaux | last post by:
>From Flanagan's JavaScript: The Definitive Guide 5th edition. 1.1.2 JavaScript is Not Simple Programmers who attempt to use JavaScript for nontrivial tasks often find the process...
7
by: Wm.M.Thompson | last post by:
For a computer programmer JavaScript is not difficult. It is pretty easy to look at some code for the first time and figure out what is going on. This is especially true if you have gratuated...
8
by: pamelafluente | last post by:
Hi guys, Is it possible to add "onload" (via Javascript) a new class to the <styleheader section? If yes, how would that be done ? <style type="text/css" media="screen"> .NewStyleClass{...
18
by: Tom Cole | last post by:
I'm working on a small Ajax request library to simplify some tasks that I will be taking on shortly. For the most part everything works fine, however I seem to have some issues when running two...
6
by: Xu, Qian | last post by:
Hello All, is there any handy tool to generate class diagrams for javascript? I have tried JS/UML, but it generates always an empty diagram. -- Xu, Qian (stanleyxu)...
23
Frinavale
by: Frinavale | last post by:
JavaScript is a very strange place for me... So, I decided that, before I attempt to create an Object, I should first learn about Objects. I had actually created one before with the help of a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.