#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    5

    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
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,920
    Rep Power
    1045
    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.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    5
    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
  6. #4
  7. No Profile Picture
    Super Moderator
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jul 2003
    Posts
    4,008
    Rep Power
    2790
    [PHP] | [Perl] | [Python] | [Java] != [JavaScript] | [XML] | [ANSI C] | [C++] | [LUA] | [MySQL] | [FirebirdSQL] | [PostgreSQL] | [HTML] | [XHTML] | [CSS]

    W3Fools - A W3Schools Intervention.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    5
    Thank you.

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

    Regards
    MG
  10. #6
  11. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,920
    Rep Power
    1045
    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.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    153
    Rep Power
    5
    Hi,

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

    Thanks.

    Regards
    MG

IMN logo majestic logo threadwatch logo seochat tools logo