473,573 Members | 3,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What's the difference between someFunc.blah = function(){} and someFunc.protot ype.blah = function(){}?

Daz
Hi everyone.

My query is very straight forward (I think).

What's the difference between

someFunc.blah = function(){ ; }

and

someFunc.protot ype.blah = function(){ ; }

? Does someFunc.blah call upon someFunc.protot ype.blah? Is there any
difference?

Many thanks.

Daz.

Dec 21 '06 #1
5 2226

Daz wrote:
Hi everyone.

My query is very straight forward (I think).

What's the difference between

someFunc.blah = function(){ ; }

and

someFunc.protot ype.blah = function(){ ; }

? Does someFunc.blah call upon someFunc.protot ype.blah? Is there any
difference?

Many thanks.

Daz.
Take a look over this:
http://www.javascriptkit.com/javatutors/oopjs.shtml
On page 2 you will find the answer you are looking for.

Dec 21 '06 #2
Daz wrote:

[snip]
What's the difference between

someFunc.blah = function(){ ; }

and

someFunc.protot ype.blah = function(){ ; }

? Does someFunc.blah call upon someFunc.protot ype.blah?
No.
Is there any difference?
Yes, a very large one.

Properties of a prototype object are only significant when the function
- in this case, someFunc - is used as a constructor function. The
prototype object becomes part of the prototype chain, affecting what
values are obtained when reading a property of the new object.

When reading a property from an object, first the object itself is
checked to determine whether it has a property with that name. If so,
the value of that property is the result. If not, the first object in
the prototype chain is checked in the same way. This repeats until
either the property has been found, or the end of the chain has been
reached, in which case the result is the value, undefined.

This can be demonstrated quite simply.

var object;

function MyObject() {}
MyObject.myProp erty = 'value';

object = new MyObject();

At this point, the function MyObject has a property named myProperty,
and an object has been created using that function. The aforementioned
property exists only on the function object -- the constructor function
-- and none of the objects created from it: the expression

typeof object.myProper ty

will evaluate to 'undefined', not 'string'.

If, instead, the prototype object is modified:

var object;

function MyObject() {}
MyObject.protot ype.myProperty = 'foo';

object = new MyObject();

different behaviour is seen. This time around, the constructor function
has no property named myProperty, but the object created using the
constructor does: the expressions

typeof object.myProper ty

and

typeof MyObject.myProp erty

will evaluate to 'string' and 'undefined', respectively. The value of
the former property is 'foo'.

Finally, it can be observed that adding a property directly to an object
obscures any value that might be obtained from the prototype chain.
Moreover, the prototype property remains unchanged:

var object, anotherObject;

function MyObject() {}
MyObject.protot ype.myProperty = 'foo';

object = new MyObject();
anotherObject = new MyObject();

object.myProper ty = 'bar';

Both objects have a property named myProperty, but the values differ:
the property of anotherObject obtains its value from the prototype
chain, whilst the property of object does not. The last assignment
statement creates a new property on the object, object, itself. In this
way, properties of a prototype object can be seen as default values for
object properties.
Keep in mind that the prototype chain is an internal property of
objects, and the chain itself cannot be changed once an object has been
created:

var object, anotherObject;

function MyObject() {}
MyObject.protot ype.myProperty = 'foo';

object = new MyObject();

/* object.myProper ty == 'foo' */

MyObject.protot ype = {myProperty: 'bar'};

anotherObject = new MyObject();

/* object.myProper ty == 'foo'
* anotherObject.m yProperty == 'bar'
*/

If changing the value of the prototype property of MyObject had an
effect on existing objects, both objects would have observed a change in
value. As it happens, only new objects are affected.

However, if references to objects in the chain are retained,
modifications to them do have an effect:

var object;

function MyObject() {}
MyObject.protot ype.myProperty = 'foo';

object = new MyObject();

/* object.myProper ty == 'foo' */

MyObject.protot ype.myProperty = 'bar';

/* object.myProper ty == 'bar' */

Hope that helps,
Mike
Dec 29 '06 #3
Daz

Michael Winter wrote:
Daz wrote:

[snip]
What's the difference between

someFunc.blah = function(){ ; }

and

someFunc.protot ype.blah = function(){ ; }

? Does someFunc.blah call upon someFunc.protot ype.blah?

No.
Is there any difference?

Yes, a very large one.

Properties of a prototype object are only significant when the function
- in this case, someFunc - is used as a constructor function. The
prototype object becomes part of the prototype chain, affecting what
values are obtained when reading a property of the new object.

When reading a property from an object, first the object itself is
checked to determine whether it has a property with that name. If so,
the value of that property is the result. If not, the first object in
the prototype chain is checked in the same way. This repeats until
either the property has been found, or the end of the chain has been
reached, in which case the result is the value, undefined.

This can be demonstrated quite simply.

var object;

function MyObject() {}
MyObject.myProp erty = 'value';

object = new MyObject();

At this point, the function MyObject has a property named myProperty,
and an object has been created using that function. The aforementioned
property exists only on the function object -- the constructor function
-- and none of the objects created from it: the expression

typeof object.myProper ty

will evaluate to 'undefined', not 'string'.

If, instead, the prototype object is modified:

var object;

function MyObject() {}
MyObject.protot ype.myProperty = 'foo';

object = new MyObject();

different behaviour is seen. This time around, the constructor function
has no property named myProperty, but the object created using the
constructor does: the expressions

typeof object.myProper ty

and

typeof MyObject.myProp erty

will evaluate to 'string' and 'undefined', respectively. The value of
the former property is 'foo'.

Finally, it can be observed that adding a property directly to an object
obscures any value that might be obtained from the prototype chain.
Moreover, the prototype property remains unchanged:

var object, anotherObject;

function MyObject() {}
MyObject.protot ype.myProperty = 'foo';

object = new MyObject();
anotherObject = new MyObject();

object.myProper ty = 'bar';

Both objects have a property named myProperty, but the values differ:
the property of anotherObject obtains its value from the prototype
chain, whilst the property of object does not. The last assignment
statement creates a new property on the object, object, itself. In this
way, properties of a prototype object can be seen as default values for
object properties.
Keep in mind that the prototype chain is an internal property of
objects, and the chain itself cannot be changed once an object has been
created:

var object, anotherObject;

function MyObject() {}
MyObject.protot ype.myProperty = 'foo';

object = new MyObject();

/* object.myProper ty == 'foo' */

MyObject.protot ype = {myProperty: 'bar'};

anotherObject = new MyObject();

/* object.myProper ty == 'foo'
* anotherObject.m yProperty == 'bar'
*/

If changing the value of the prototype property of MyObject had an
effect on existing objects, both objects would have observed a change in
value. As it happens, only new objects are affected.

However, if references to objects in the chain are retained,
modifications to them do have an effect:

var object;

function MyObject() {}
MyObject.protot ype.myProperty = 'foo';

object = new MyObject();

/* object.myProper ty == 'foo' */

MyObject.protot ype.myProperty = 'bar';

/* object.myProper ty == 'bar' */

Hope that helps,
Mike
Thanks a lot for that Mike! It's really helped me out a lot. I also
heard that using prototype is more effective than declaring the
properties/methods inside of the function, as when prototype is used,
each new instance of the object is simply updated with a reference to
the new prototype, rather than having a fresh instance of each object,
inside of the newly created objects (if that makes sense).

Basically, using prototypes can save RAM usage, and hopefully speed the
objects up marginally.

Very useful stuff.

Thanks again. :)

Daz.

Dec 29 '06 #4
Daz wrote:

[snip]
Thanks a lot for that Mike! It's really helped me out a lot.
My apologies that I took so long to reply. It's been on my to-do list
for a while.
I also heard that using prototype is more effective than declaring the
properties/methods inside of the function, as when prototype is used,
each new instance of the object is simply updated with a reference to
the new prototype, rather than having a fresh instance of each object,
inside of the newly created objects (if that makes sense).
You're referring to

function MyObject() {
this.method = function () {
/* ... */
};
}

versus

function MyObject() {}
MyObject.protot ype.method = function () {
/* ... */
};

right?

When a function expression is evaluated, a new function object is
created. In the first of the two snippets above, the function expression
inside the body of MyObject will be evaluated every time MyObject is
invoked -- every time a new object is created from MyObject. In the
second, the function expression is only evaluated once.
Basically, using prototypes can save RAM usage, and hopefully speed the
objects up marginally.
Memory, certainly; construction time, as well. Obtaining the value of
the property will take slightly longer though as it becomes necessary to
search the prototype chain rather than finding the property immediately
on the object itself. As trade-offs go, it's not a bad one.

There are, of course, circumstances where the first approach is best:
when a closure is needed for some reason. This is typically when using
"private" members.
Very useful stuff.

Thanks again. :)
You're welcome. :-)

Mike
Dec 29 '06 #5
Daz

Michael Winter wrote:
My apologies that I took so long to reply. It's been on my to-do list
for a while.
No problem. I am just greatful for your help.
I also heard that using prototype is more effective than declaring the
properties/methods inside of the function, as when prototype is used,
each new instance of the object is simply updated with a reference to
the new prototype, rather than having a fresh instance of each object,
inside of the newly created objects (if that makes sense).

You're referring to

function MyObject() {
this.method = function () {
/* ... */
};
}

versus

function MyObject() {}
MyObject.protot ype.method = function () {
/* ... */
};

right?
Yes, indeed.
When a function expression is evaluated, a new function object is
created. In the first of the two snippets above, the function expression
inside the body of MyObject will be evaluated every time MyObject is
invoked -- every time a new object is created from MyObject. In the
second, the function expression is only evaluated once.
Basically, using prototypes can save RAM usage, and hopefully speed the
objects up marginally.

Memory, certainly; construction time, as well. Obtaining the value of
the property will take slightly longer though as it becomes necessary to
search the prototype chain rather than finding the property immediately
on the object itself. As trade-offs go, it's not a bad one.
Oh I see. So I guess it depends on the application of the code as to
which one would be best to use, as always.
There are, of course, circumstances where the first approach is best:
when a closure is needed for some reason. This is typically when using
"private" members.
Precisely. Thank you so much for your input again. If I can remember
even 50% of it, then I am off to a good start. Some day I will become a
JavaScript guru. I just hope that day comes before I die. Hehe.

I would like to wish you a happy new year, in advance.

Daz.

Dec 29 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2253
by: Nobody | last post by:
Lets say I have a class that is only available if a specific DLL (a.dll) is present. I can't link to that DLL through lib files or my app will fail on any machine that doesn't have a.dll. So I do LoadLibrary()'s and keep function pointers in my wrapper class... Which is a better style? DWORD CClass::SomeFunc() { if (m_pfn != NULL)
23
22158
by: Ian Tuomi | last post by:
Hello, I was wondering, does it make any difference if you write void foo(int x) { /* insert code here */ } or foo(int x)
5
2528
by: noblesantosh | last post by:
Hi all, What is the difference between following two function definations? <1> void func(void) { /* some code */ } <2> void func()
9
3089
by: pamelafluente | last post by:
Hi, I was "studying" the famous (public code) BusyBox. I see the instruction: var busyBox = new BusyBox as in var busyBox = new BusyBox("BusyBox1", "busyBox", 4, "gears_ani_", ".gif", 125, 147, 206)
6
2194
by: Charles Sullivan | last post by:
I define and initialize an array of structures like the following, (where the <verbiage within angle bracketsis just meant to be explanatory): int func1(<argument prototypes>); int func2(<argument prototypes>); struct mystruct { <other stuff>; int (*myfunc)();
15
9002
by: robert maas, see http://tinyurl.com/uh3t | last post by:
Here's the source: #include <stdio.h> #include <errno.h> main () { char* str = "9999999999"; long long int llin; char* endptr; /* Set by strtoll */ int nch; errno = 0; llin = strtoll(str, &endptr, 10); printf("errno=%d\n", errno);
92
6138
by: Heinrich Pumpernickel | last post by:
what does this warning mean ? #include <stdio.h> int main() { long l = 100; printf("l is %li\n", l * 10L);
1
1463
by: dennis.sprengers | last post by:
I've compared some open-source javascript editors, and found different techniques for constructing the code. Two examples: CodePress.run = function() { new CodePress(t); } CodePress = function(obj) { self.initialize = function() {} self.toggleLineNumbers = function() {}
4
2112
by: Gestorm | last post by:
Hi all, I found a macro "USE_VAR" in the code of bash-3.2 as follows: /*file: bash-3.2/shell.c*/ 344 USE_VAR(argc); 345 USE_VAR(argv); 346 USE_VAR(env); 347 USE_VAR(code); 348 USE_VAR(old_errexit_flag); 349 #if defined (RESTRICTED_SHELL) 350 USE_VAR(saverst);
0
7741
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7661
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8165
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7730
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6347
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5252
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2163
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1256
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.