Javascript: what's colon operator in variable name? - Stack Overflow

i have code like this:var db: name = dbFunction(true);dbFunction returning Object.I have question, what

i have code like this:

var db: name = dbFunction(true);

dbFunction returning Object.

I have question, what doing this colon operator in variable name?

i have code like this:

var db: name = dbFunction(true);

dbFunction returning Object.

I have question, what doing this colon operator in variable name?

Share Improve this question edited Nov 21, 2011 at 11:45 Matt 75.3k26 gold badges156 silver badges180 bronze badges asked Nov 21, 2011 at 11:38 Marcin KarkochaMarcin Karkocha 412 bronze badges 3
  • It is causing a syntax error to be thrown. – Quentin Commented Nov 21, 2011 at 11:41
  • 1 I have code created by IBM and i dont think it's syntax error – Marcin Karkocha Commented Nov 21, 2011 at 12:20
  • I've inherited some working code with something similar const store: Object = configureStore(), and am similarly confused. – mstringer Commented Feb 13, 2017 at 22:02
Add a ment  | 

3 Answers 3

Reset to default 5

It's a high tech operator that guarantees a syntax error when used like that.

In it's normal use, you might see it used in object literal syntax to denote key:value pairs;

var object = {
    "name": "value",
    "name2": "value2"
}

It can also be used to define a label (less mon).

loop1:  
for (var i=0;i<10; i++) {
   for (var j=0;j<10;j++) {
      break loop1; // breaks out the outer loop
   }  
}   

And it's part of the ternary operator;

var something = conditional ? valueIfTrue : valueIfFalse;

The colon has several uses in JavaScript.

  1. It's used to separate keys from values in the JSON notation.
var db = {
    name: dbFunction(name)
};
  1. It's the ternary operator:

var db = (1 == 1 ? true : false);

  1. Labels aka GOTO. Stay away from them.

It is also used in switch cases:

switch(product) {
    case "apple":
        return "Yum";
        break;
    case "orange":
        return "juicy!";
        break;
    case "milk":
        return "cold!";
        break;
}

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

相关推荐

  • Javascript: what&#39;s colon operator in variable name? - Stack Overflow

    i have code like this:var db: name = dbFunction(true);dbFunction returning Object.I have question, what

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信