0

I have a foo object as follow :

var foo =
{   
    "timer" : 
    {
        "hours" : 0, 
        "minutes" : 0, 
        "seconds" : 0,  
        "time" : new Date(1970, 0, 1, foo.timer.hours, foo.timer.minutes, foo.timer.seconds).getTime()                      
    }
};

The problem is that foo.timer.hours, foo.timer.minutes and foo.timer.seconds properties in new Date() aren't recognized because javascript console in chrome browser displays that :

Uncaught TypeError: Cannot read property 'timer' of undefined

So why foo.timer.hours, foo.timer.minutes and foo.timer.seconds properties in new Date() aren't recognized ?

2
  • Where do you log foo.timer.seconds Commented Jun 2, 2018 at 14:42
  • Timer of undefined means, parent of timer, ie, foo is going undefined where ever you are trying to access it. Commented Jun 2, 2018 at 14:42

5 Answers 5

2

foo isn’t defined until the end of the var foo = { ... }; statement. You could define these variables before defining foo.

var hours = 0, minutes = 0, seconds = 0;
var foo = {
    timer: {
        hours, minutes, seconds,
        time: new Date( 1970, 0, 1, hours, minutes, seconds ).getTime()
    }
};
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, you're right, foo isn't defined until the end of the var foo = {...}
This is ugly. Doesn't allow storing the values in the object itself the way original intent seems to be
Yeah I agree, the suggestions in other answers are better.
1

The way you are creating the foo object is incorrect... first create the object and use its properties, you are trying to refer the foo object in your time property when the variable foo hasn't been asigned yet..

var foo =
{   
    "timer" : 
    {
        "hours" : 0, 
        "minutes" : 0, 
        "seconds" : 0,  
                            
    }
};

foo["time"] = new Date(1970, 0, 1, foo.timer.hours, foo.timer.minutes, foo.timer.seconds).getTime();

console.log(foo)

Comments

1

foo.time = new Date(1970, 0, 1, foo.timer.hours, foo.timer.minutes, foo.timer.seconds).getTime()

you can do this.

Comments

1

You are trying assign some values before those are initialize. You should split the declaration of the values and then you can add the time key/value function.

Hope this helps :>

var foo =
{   
    "timer" : 
    {
        "hours" : 0, 
        "minutes" : 0, 
        "seconds" : 0                   
    }
};

foo.timer['time'] = new Date(1970, 0, 1, foo.timer.hours, foo.timer.minutes, foo.timer.seconds).getTime();   
console.log(foo)

Comments

1

An alternate approach is add a getter to the object

var foo =
{   
    "timer" : 
    {
        "hours" : 0, 
        "minutes" : 0, 
        "seconds" : 0, 
        get time(){
           return new Date(1970, 0, 1, this.hours, this.minutes, this.seconds).getTime()
        }                       
    }
};

console.log(foo.timer.time)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.