The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> JavaScript Development
|
Other - Needs some clarification on JS objects please
Discuss Needs some clarification on JS objects please in the JavaScript Development forum on Dev Shed. Needs some clarification on JS objects please JavaScript Development forum discussing JavaScript and DHTML, AJAX, and issues such as coding cross-browser JavaScript.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 15th, 2012, 06:02 AM
|
|
Contributing User
|
|
Join Date: Aug 2009
Posts: 153
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
|

November 15th, 2012, 07:27 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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.
|

November 15th, 2012, 07:42 AM
|
|
Contributing User
|
|
Join Date: Aug 2009
Posts: 153
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
|

November 15th, 2012, 07:44 AM
|
|
|
|

November 15th, 2012, 07:55 AM
|
|
Contributing User
|
|
Join Date: Aug 2009
Posts: 153
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
|

November 15th, 2012, 08:26 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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.
|

November 15th, 2012, 09:07 AM
|
|
Contributing User
|
|
Join Date: Aug 2009
Posts: 153
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|