最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to reference a key from the same object when creating it? - Stack Overflow

programmeradmin2浏览0评论

Say I have an object such as this:

var time = {
    'second': 1000,
    'minute': 1000 * 60,
    'hour'  : 1000 * 60 * 60,
    'day'   : 1000 * 60 * 60 * 24,
    'week'  : 1000 * 60 * 60 * 24 * 7
};

How would I shorten it so it would reference the other keys?

I mean something like this:

var time = {
    'second': 1000,
    'minute': this.second * 60,
    'hour'  : this.minute * 60,
    'day'   : this.hour   * 24,
    'week'  : this.day    * 7
}

I can't reference each using time.foo because the variable isn't initialized yet. I could simply add each on a new command, such as time['hour'] = time.minute * 60;, but I'd rather do it in one command.

Say I have an object such as this:

var time = {
    'second': 1000,
    'minute': 1000 * 60,
    'hour'  : 1000 * 60 * 60,
    'day'   : 1000 * 60 * 60 * 24,
    'week'  : 1000 * 60 * 60 * 24 * 7
};

How would I shorten it so it would reference the other keys?

I mean something like this:

var time = {
    'second': 1000,
    'minute': this.second * 60,
    'hour'  : this.minute * 60,
    'day'   : this.hour   * 24,
    'week'  : this.day    * 7
}

I can't reference each using time.foo because the variable isn't initialized yet. I could simply add each on a new command, such as time['hour'] = time.minute * 60;, but I'd rather do it in one command.

Share Improve this question edited Sep 12, 2012 at 21:42 Santiago Elvira Ramirez 1,1918 silver badges28 bronze badges asked Sep 12, 2012 at 21:37 TgwizmanTgwizman 1,5382 gold badges19 silver badges34 bronze badges 5
  • This cannont be done with a single Object Literal.. There are a number of duplicates, but finding them can be a PITA sometimes. – user166390 Commented Sep 12, 2012 at 21:45
  • 3 FYI, your code is probably more readable if you keep the code from your first example; possibly with 60 * 60 written as 3600 (everyone knows that's an hour). – ThiefMaster Commented Sep 12, 2012 at 21:47
  • 1 Similar: stackoverflow.com/questions/4616202/… , stackoverflow.com/questions/7433395/… , stackoverflow.com/questions/2787245/… – user166390 Commented Sep 12, 2012 at 21:48
  • Also similar: stackoverflow.com/q/4858931/615754 – nnnnnn Commented Sep 12, 2012 at 21:50
  • @pst Thanks for pointing that out. I actually had trouble on finding one like the question I had. I have no idea what to search for when looking for a duplicate to this question, considering I just couldn't find the right words to describe what I was going for (hence the long title). – Tgwizman Commented Sep 12, 2012 at 21:58
Add a comment  | 

5 Answers 5

Reset to default 7

you can't reference properties from itself if you are declaring it like that as the properties dont yet exist, you might want to try a different approach

var time = {};
time.second = 1000;
time.minute = time.second * 60;
...
var time = (function(){
    var second = 1000,
        minute = second * 60,
        hour = minute * 60,
        day = hour * 24,
        week = day * 7;
    return {
        'second': second,
        'minute': minute,
        'hour'  : hour,
        'day'   : day,
        'week'  : week
    };
})();

UPD

Even shorter:

var time = (function(){
    var w, d, h, m, s;
    w = (d = (h = (m = (s = 1000) * 60) * 60) * 24) * 7;
    return {
        second: s,
        minute: m,
        hour  : h,
        day   : d,
        week  : w
    };
})();

And finally the minified version of this:

var time=function(){var a,b,c,d,e;a=(b=(c=(d=(e=1e3)*60)*60)*24)*7;return{second:e,minute:d,hour:c,day:b,week:a}}()

You can't do this as the object does not exist yet while it's parsed.

The only way to avoid this is creating an object and then adding the keys one by one - so you already have an object which has the properties you want to re-use.

Change the object to a constructor function and you can do this:

function Time() {
    this.second = 1000;
    this.minute = this.second * 60;
    this.hour = this.minute * 60;
    this.day = this.hour * 24;
    this.week = this.day * 7;
}

// Use
var time = new Time();

You can use the getters syntax supported by most modern browser :

var time = {
    'second': 1000,
     get minute () {
       return this.second * 60;
     },
     get hour (){
       return this.minute * 60;
     },
     get day (){
       return this.hour * 24;
     },
     get week (){
       return this.day * 7;
     }
};
发布评论

评论列表(0)

  1. 暂无评论