Passing variables into an Object in Javascript - Stack Overflow

I am trying to pass a variable into an Object so that the property's value is the variable I passe

I am trying to pass a variable into an Object so that the property's value is the variable I passed in.

var car = {
  make: 'Jeep',
  model: 'Renegade',
  year: yearVar
}

var yearVar = 2016;
console.log(car.year); //says year is undefined

So how do I set the property of the car Object equal to the yearVar variable?

I am trying to pass a variable into an Object so that the property's value is the variable I passed in.

var car = {
  make: 'Jeep',
  model: 'Renegade',
  year: yearVar
}

var yearVar = 2016;
console.log(car.year); //says year is undefined

So how do I set the property of the car Object equal to the yearVar variable?

Share Improve this question edited Jan 30, 2017 at 17:28 donald stouffer asked Jan 30, 2017 at 17:18 donald stoufferdonald stouffer 111 silver badge4 bronze badges 3
  • 1 There is no error. Have you tried swapping the positions of var car = … and var yearVar = …? – Sebastian Simon Commented Jan 30, 2017 at 17:19
  • I left the console.log at the bottom and moved the declaration of the variable above the object and that fixed it. Thanks! – donald stouffer Commented Jan 30, 2017 at 17:25
  • So many answers for a simple problem of setting the value of the variable after using it. I feel so left out! – rasmeister Commented Jan 30, 2017 at 17:28
Add a ment  | 

5 Answers 5

Reset to default 3

undefined is not an error. It's saying undefined because at the line you assign the value of yearVar to the object, the variable yearVar is not defined yet so it will assign undefined instead. (defining the variable afterwards won't solve the problem because undefined is already assigned). What you need to do is:

// define yearVar first
var yearVar = 2016;

var car = {
  make: 'Jeep',
  model: 'Renegade',
  year: yearVar // then use it afterwards (after it's been defined)
}

Declare variable yearVar and assign its value before you create car object.

var yearVar = 2016;
var car = {
  make: 'Jeep',
  model: 'Renegade',
  year: yearVar
}
console.log(car.year)

You will need to declare and set the yearVar varible before defining the car object. Javascript executes top to bottom Doing something like this would surely work

var yearVar = 2016;

var car = {
  make: 'Jeep',
  model: 'Renegade',
  year: yearVar
}

console.log(car.year);

Though the problem is solved I am sharing something new but it is correct. This is a technique used by the JavaScript engine called hoisting.

yearVar = 2016; //check the var declaration in the bottom
var car = {
  make: 'Jeep',
  model: 'Renegade',
  year: yearVar
}

var yearVar;
console.log(car.year); 

To change after car has been created:

var car = {
  make: 'Jeep',
  model: 'Renegade',
  year:null
}

car.year = 2016;

console.log(car.year);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744751729a4591638.html

相关推荐

  • Passing variables into an Object in Javascript - Stack Overflow

    I am trying to pass a variable into an Object so that the property's value is the variable I passe

    16小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信