JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2012, 06:02 AM
mind_grapes mind_grapes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 153 mind_grapes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 5 m 54 sec
Reputation Power: 4
Other - Needs some clarification on JS objects please

Hello All,

Again, not wishing to beat the "new to JS" drum, but i really am, and so, im after some clarification about objects, namely the ability to add methods to objects that have been created within a function (i think that's the right expression?)

Here is the example im working with - found over at w3s:

Code:
<script>
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
 
this.changeName=changeName;
function changeName(name)
{
this.lastname=name;
}
}
myMother=new person("Sally","Rally",48,"green");
myMother.changeName("Doe");
document.write(myMother.lastname);
</script>


While I understand the ability to amend the last name here, the question i have is:

Q. can you modify the firstname within the same function - dont know how this would be done - or do you have to create a new function all together like this?

Code:
<script>
function person(firstname,lastname,age,eyecolor)
{
this.firstname=firstname;
this.lastname=lastname;
this.age=age;
this.eyecolor=eyecolor;
 
this.changeName=changeName;
this.changeFirst=changeFirst;

function changeName(name)
{
this.lastname=name;
}

function changeFirst(fname)
{
this.firstname=fname
}
}

myMother=new person("Sally","Rally",48,"green");
myMother.changeName("Doe");
myMother.changeFirst(" blah blah");

document.write(myMother.lastname + myMother.firstname);
</script>


this was my attempt as i dont know how this would be achieved within the first code.

Apologies if this is trivial, and not what the forums are for.

Your help is most appreciate

Kind regards
MG

Reply With Quote
  #2  
Old November 15th, 2012, 07:27 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,864 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 20 h 35 m 35 sec
Reputation Power: 813
Hi,

first of all: Don't use w3schools for learning. It's known to be a bad resource that contains a lot of false information and bad advice (like using document.write()).

As to your question:

You can change and add object properties wherever and whenever you want. For example, you could directly change the firstname property on the top level outside of any function/method:

Code:
myMother.firstname = 'xyz';


And of course you can also change the firstname in the changeName() method:

Code:
function changeName(first, last)
{
	this.firstname = first;
	this.lastname = last;
}


Just try it out.

Reply With Quote
  #3  
Old November 15th, 2012, 07:42 AM
mind_grapes mind_grapes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 153 mind_grapes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 5 m 54 sec
Reputation Power: 4
Hi,

thanks for the reply, and the advice.

Can you recommend any other sites please? is tizag any good? i never know which one to use so i default to w3s.

ha, you're right:

Code:

function changeName(first, last)
{
this.firstname=first;
this.lastname=last;
}
}
myMother=new person("Sally","Rally",48,"green");
myMother.first="first "
myMother.last="last"
document.write(myMother.first + myMother.last);


Thanks once again.

Kind regards
MG

Reply With Quote
  #4  
Old November 15th, 2012, 07:44 AM
Winters Winters is offline
Super Moderator
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jul 2003
Posts: 3,871 Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level)Winters User rank is General 24th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 16 h 31 m 24 sec
Reputation Power: 2569
__________________
[PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

W3Fools - A W3Schools Intervention.

Reply With Quote
  #5  
Old November 15th, 2012, 07:55 AM
mind_grapes mind_grapes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 153 mind_grapes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 5 m 54 sec
Reputation Power: 4
Thank you.

W3Schools had me fooled. I thought they were a part of W3C ! ! Doh!

Regards
MG

Reply With Quote
  #6  
Old November 15th, 2012, 08:26 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,864 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 20 h 35 m 35 sec
Reputation Power: 813
Quote:
Originally Posted by mind_grapes
W3Schools had me fooled. I thought they were a part of W3C ! ! Doh!


Yeah, that's probably what everyone thinks at first. And that's hardly a coincidence. They work hard to make it look like they're some "official" reference to sell their damn "certificates".

By the way, be careful with semicolons in your code. You forgot them in the two lines where you set the properties.

Reply With Quote
  #7  
Old November 15th, 2012, 09:07 AM
mind_grapes mind_grapes is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 153 mind_grapes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 5 m 54 sec
Reputation Power: 4
Hi,

Yeah, i keep forgetting certain characters. Ill try to remember in future.

Thanks.

Regards
MG

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Other - Needs some clarification on JS objects please

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap