How to reference a key from the same object when creating it?

前端 未结 5 1855
谎友^
谎友^ 2021-01-19 15:26

Say I have an object such as this:

var time = {
    \'second\': 1000,
    \'minute\': 1000 * 60,
    \'hour\'  : 1000 * 60 * 60,
    \'day\'   : 1000 * 60 *          


        
5条回答
  •  鱼传尺愫
    2021-01-19 15:58

    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();
    

提交回复
热议问题