0

Update: Thanks @Mathletics - I used an = when I should have used a : inside the object literal notation. setAge: setAge works perfectly inside the object literal.

In the Codecademy JavaScript track, the following problem occurs:

// here we define our method using "this", before we even introduce bob
var setAge = function (newAge) {
  this.age = newAge;
};
// now we make bob
var bob = new Object();
bob.age = 30;
bob.setAge = setAge;

// make susan here, and first give her an age of 25
//var susan = {age: 25, setAge = setAge}; //does not work
var susan = new Object();
susan.age = 25;
susan.setAge = setAge;

// here, update Susan's age to 35 using the method
susan.setAge(35);

I want to set susan.setAge to the global function, setAge. When I try to do it using object literal notation it does not work, but if I use new and then use dot notation it does work.

I tried setAge = global.setAge inside the object literal notation as well, but that failed. setAge = this.setAge, function setAge = setAge, function setAge = this.setAge and function setAge = global.setAge all also failed.

I tried researching related questions on Stack Overflow but those weren't very related. And this question, while about a similar example, seems to be about the author's confusion between creating a method within an object and calling a global method using .apply(), which he or she does not appear to be aware of.

Community
  • 1
  • 1
phoenixdown
  • 828
  • 1
  • 10
  • 16
  • 3
    Did you actually use `=` instead of `:` inside your object? – Evan Davis Jul 07 '14 at 19:41
  • Ah that's a shame...well spotted! If you make an answer I'd be glad to confirm it. – phoenixdown Jul 07 '14 at 19:43
  • voting to close - it's just a trivial typographical error, not a real problem. – Alnitak Jul 07 '14 at 19:44
  • downvoting here is unhelpful. i tried my best here, doing research and trying different code examples etc. this is not the most helpful attitude to have towards newcomers of the site, who are probably just as likely as everyone else to make a typographical error. codecademy's compiler doesn't tell me where the error in the code is (and neither does jsfiddle, or at least i haven't figured out how to get it to do that), so this was rather petty. – phoenixdown Jul 07 '14 at 19:46
  • @bobo you would have had an error in the console if you opened it and checked. And it's not a downvote, it's a vote to close. – Evan Davis Jul 07 '14 at 20:02
  • @Mathletics - someone else downvoted me, and apparently took back their downvote after my comment, so it seems like it was effective (though it appears you have added a downvote of your own). No problem with the vote to close from me, though juvian's answer seems pretty helpful right now, and I still have some questions about it, so leaving it up for now. – phoenixdown Jul 07 '14 at 20:43

1 Answers1

1

Your sintax is wrong:

var susan = {age: 25, setAge : setAge}; // : instead of =
susan.setAge(35);
console.log(susan.age) // will show 35

You could also try a different approach:

function Person(params){
    params=params || {};
    this.age=params.hasOwnProperty("age") ? params.age : null;

    this.setAge=function(age){
        this.age=age;
    }
}

var susan = new Person({age:25});
susan.setAge(35);
console.log(susan.age) // outputs 35

As you have mentioned, using arguments saves a few lines, as its always an object:

function Person(){
    this.age=arguments.hasOwnProperty("age") ? arguments["age"] : null;
    this.setAge=function(age){
        this.age=age;
    }
}
juvian
  • 15,875
  • 2
  • 37
  • 38
  • thanks for the response, @juvian, a couple questions: how does javascript know where to look when you write `["age"]` as the first value in your conditional? also, why can you not use `arguments[]` instead of the `params` value you've made? and what does it mean to have an OR in your assignment: `params=params || {};` thanks – phoenixdown Jul 07 '14 at 20:42
  • I haven't been able to find them. Could you provide links? Here are the search terms I used for each question: 1. I don't know what to search for for this question that is distinct from my queries for the second question. 2. `arguments vs params`, `arguments vs params javascript` 3. `or in assignment `, `var = ||`, `var = || javascript`, `or in assignment javascript` It's interesting how many SO members offer a practiced unhelpfulness to newer members. – phoenixdown Jul 07 '14 at 20:55
  • @bobo Sorry my mistake! Should have been `params["age"]` , already fixed it. Not used to use arguments, but yeah could use them. Regarding the `params=params || {};` , its just in case you do `var susan = new Person();` so if you do not sent any param(params is undefined), I set it to an empty object for it to work. Params is just the name I chose for the object, could be named data as well. Using arguments it would save a few lines, check edited answer – juvian Jul 08 '14 at 05:44
  • thanks @juvian! as an fyi, the comment above yours was addressed to another user, who appears to have withdrawn his comment. thank you for your help and kindness both! – phoenixdown Jul 09 '14 at 21:15