0

I am very new to javascript and have started learning recently. I am not able to assign the variable to correct value. Below is the code:

person={
firstname: fnName,
lastname:"Doe",
age:50,
eyecolor:"blue"
}

var fnName=(function(){
    return 'John';
})();

console.log(person.firstname + " is " + person.age + " years old.");

The output should be John is 50 years old. The jsfiddle link is http://jsfiddle.net/prashdeep/Fj6Xf/

Please let me know where I am going wrong?

Pradeep
  • 713
  • 6
  • 14
  • 25
  • Define your function first then use it, [here][1] is a good explanation. [1]: http://stackoverflow.com/questions/336859/var-functionname-function-vs-function-functionname – KKK Mar 26 '14 at 05:06

5 Answers5

3

You're trying to use fnName before you assign a value to it. Move the var fnName = line to before the person= line, like this:

var fnName=(function(){
    return 'John';
})();

person={
    firstname: fnName,
    lastname:"Doe",
    age:50,
    eyecolor:"blue"
}

console.log(person.firstname + " is " + person.age + " years old.");
Carter Sande
  • 1,619
  • 11
  • 25
2

The trailing parenthesis make your function call itself immediately, but that's after you've created your person object - at the time of its creation, fName was undefined, hence your error. Switch the order around.

var fnName=(function(){
  return 'John';
})();

  person={
  firstname: fnName,
  lastname:"Doe",
  age:50,
  eyecolor:"blue"
}
Gary
  • 11,083
  • 14
  • 43
  • 68
2

Try:

var fnName = function() {
    return 'John';
}

var person = {
    firstname: fnName,
    lastname: "Doe",
    age: 50,
    eyecolor: "blue"
}

console.log(person.firstname + " is " + person.age + " years old.");

That way the object "person" will know what fName is. Also you don't need your function to call itself when the code is first executed. Format your function like the code above.

Alec Moore
  • 1,079
  • 2
  • 10
  • 20
2

you can write a class after that we can call a method through object. like this

function person() {
this.firstname = fnName;
this.lastname = "Doe";
this.age = 50;
this.eyecolor = "blue";
}

var fnName = (function () {
    return 'John';
})();

var obj = new person();

console.log(obj.firstname + " is " + obj.age + " years old.");
Phoenix
  • 1,167
  • 1
  • 8
  • 22
1

first you are using function as argument so it should be

var fnName = function(){return 'john'}; and also you should also know the execution pattern of javascript that is person.fname will be undefined so insted of defining the function below your object define at the top and do not invoke function instantly after defining that function because you need to get the value not fnname not to invoke there only.

also the object should also not to be defined like that you have defined if you do so then it will throw an exception so to define an object it format is

    var objectname = {
      propertyname : propertyvalue,
      propertyname : propertyvalue,
      propertyname : {
        anotherpropertname : anotherpropertyvalue,
        anotherpropertname : anotherpropertyvalue
  }

    };

remember when you are on the last property then dont use comma as you can see in the above example. object can also have a value as a function.

in your first property of your object person insted of define a function fnName seprately you could also written as

var person = {
  firstname : function(){return 'john'}
};

another intresting thing is that window is also an object that is you can also define an object like this window["person"] ={fanme:FnName, age:50} and so on..

anni
  • 290
  • 1
  • 3
  • 15