简体   繁体   中英

Single colon in JavaScript as a prefix to a variable (not object literal)

In Chrome you can do:

date = new Date();

and then in the console you can do:

hour:date.getHours();

What is this called? Where else does it work?

I saw this in the follow code:

showDateTimePicker(date, callback) {
    date = date || new Date();
    var options = {
        ...this.props,
        year:date.getFullYear(),
        month:date.getMonth(),
        day:date.getDate(),
        hour:date.getHours(),
        minute:date.getMinutes()
    };
    RCTDateTimePicker.showDateTimePicker(options, function (year, month, day, hour, minute) {
        date.setFullYear(year);
        date.setMonth(month);
        date.setDate(day);
        date.setHours(hour);
        date.setMinutes(minute);
        callback(date);
    });
}

hour:date.getHours(); and var options = {hour:date.getHours()}; are two very different statements.

The former is a label which is designed so that when you have nested loops and want to break or continue from one of them, you can specify which. Putting it before a function call is useless.

The latter is an object initialiser which allows you to specify the name and values of properties on a new object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM