Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-26-2012, 11:53 AM   PM User | #1
LiveFree
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
LiveFree is an unknown quantity at this point
Difference between this property and prototype property?

Can someone tell me the difference between having a property declared on the function level or the prototype level? They both seem to work the same to me...

If I create two Persons (see below) and change the firstName on one of them, it updates only that instance of the person (as expected), and not the other, no matter the implementation (this or prototype), so why choose one or the other? Or does it matter?

Example:
Code:
var Person = function() {
      this.firstName = "bob";
}
or

Code:
var Person = function(){};
Person.prototype.firstName = "bob";
Run either implementation returns the same result.
Code:
function Test() {
      var p1 = new Person();
      var p2 = new Person();
      p1.firstName = "jane";
      alert(p1.firstName); //bob
      alert(p2.firstName); //jane;
}

Thanks for the help!

Last edited by LiveFree; 08-26-2012 at 11:59 AM..
LiveFree is offline   Reply With Quote
Old 08-26-2012, 03:32 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
The two codes are functionally the same.

As we often say here, there are several different ways of killing a cat. In many instances there is no discernable advantage of using one over another.

Quizmaster: What is the only prime number between 60 and 65?
Contestant: 50.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 08-26-2012, 07:21 PM   PM User | #3
LiveFree
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
LiveFree is an unknown quantity at this point
My understanding is that prototype reduces memory by not cloning methods to classes implementing that prototype.

How does that work with properties like in this example? Does it copy non-functions to the instance object? It seems like it does, but I'm kind of a newbie so I'm unsure...
LiveFree is offline   Reply With Quote
Old 08-26-2012, 10:44 PM   PM User | #4
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,463
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
When you define a property directly on the object the prototype property becomes inaccessible (unless you specifically reference it via the prototype).

If you don't define the property directly (or delete the direct property) then the prototype property with the same name will be accessed instead.

Code:
var Person = function(){};
Person.prototype.firstName = "bob";
function Test() {
      var p1 = new Person();
      var p2 = new Person();
      p2.firstName = "jane";
      alert(p1.firstName); //bob - reference to prototype
      alert(p2.firstName); //jane - direct reference
      delete p2.firstName;
      alert(p2.firstName); //bob - reference to prototype
}
The prototype property occurs once regardless of how many objects use it. The individual objects can either have their own copy defined or use the prototype one.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/

Last edited by felgall; 08-27-2012 at 03:13 AM..
felgall is online now   Reply With Quote
Old 08-26-2012, 10:49 PM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
I think it should be

p2.firstName = "jane";

for the above example.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS
AndrewGSW is offline   Reply With Quote
Old 08-26-2012, 11:31 PM   PM User | #6
LiveFree
New to the CF scene

 
Join Date: Aug 2012
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
LiveFree is an unknown quantity at this point
Ah, thank you for the help
LiveFree is offline   Reply With Quote
Old 08-27-2012, 04:57 AM   PM User | #7
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by LiveFree View Post
My understanding is that prototype reduces memory by not cloning methods to classes implementing that prototype.

How does that work with properties like in this example? Does it copy non-functions to the instance object? It seems like it does, but I'm kind of a newbie so I'm unsure...
i like to think of prototypes as globals and this as a local.
prototype.x is the exact same object for all, this.x is specific to the function executing the code.
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 08-27-2012, 05:09 AM   PM User | #8
Logic Ali
Regular Coder

 
Logic Ali's Avatar
 
Join Date: Sep 2010
Location: London
Posts: 961
Thanks: 0
Thanked 198 Times in 193 Posts
Logic Ali will become famous soon enoughLogic Ali will become famous soon enough
Quote:
Originally Posted by rnd me View Post
prototype.x is the exact same object for all, this.x is specific to the function executing the code.
Don't you mean the instance of the object executing the code?
Logic Ali is offline   Reply With Quote
Old 08-27-2012, 05:15 AM   PM User | #9
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
If we were using a class-oriented language, you would say that the prototype properties are static properties. But then class-oriented languages don't let you add properties to individual objects, so the analogy quickly breaks down.

It's kind of funny, back in 1995 when I was helping build a combination C++/Java development system/compiler (yeah, I know...Java *compiler*?), the compiler guru that was guiding the thing had the idea of adding the ability to Java (and C++!) to dynamically add properties to objects (not classes). We ended up deciding that we could add it later, but then the product died. Too bad. If we'd pulled it off then we'd have had a language that was both class-oriented and object-oriented. (The sad part is it wouldn't have really been all that hard to do. The base Object class would have had methods addProperty and addMethod, very similar to, say, the DOM's setAttribute concept.)
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-27-2012, 05:16 AM   PM User | #10
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by Logic Ali View Post
Don't you mean the instance of the object executing the code?
But functions are objects in JavaScript.

[I just had to throw that in. Purely tongue in cheek.]
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 08-27-2012, 06:16 AM   PM User | #11
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,468
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Quote:
Originally Posted by rnd me View Post
prototype.x is the exact same object for all, this.x is specific to the function executing the code.
Quote:
Originally Posted by Logic Ali View Post
Don't you mean the instance of the object executing the code?
no. well, if you are directly invoking a function as a method of an object created using "new ", then this will equal the object instance, as you note. that's true, but i was alluding to more.

this is defined by the function's activation context, which means .call(), .apply(), .bind(), and even [1,2,3].map( fn , thisObject ) can all set what this means.


you can use this to make lightweight prototype-less 'instances' by applying a Constructor instead of using new:

Code:
function Person() {
      this.firstName = "bob";
}

Person.prototype.age=123;

var o={};
Person.call(o);


alert([o.firstName, o.age ]); //shows: "bob," (no age)
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Old 08-27-2012, 06:53 AM   PM User | #12
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,237
Thanks: 59
Thanked 3,998 Times in 3,967 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Oogle. You do like to explore the edges, don't you. But, yes, that all makes perfect sense. Thanks.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Tags
properties, property, prototype

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:43 AM.


Advertisement
Log in to turn off these ads.