js对象var varis ={ int : x=2, int : y=3 }what does it mean?

代码如下,为什么下面两个格式都不会报错,都是什么意思。这么使用会不会有什么问题
The code is as follows. What do they mean? Will there be any problem with this use?

//问题一
//这种方式在获取的时候可以直接访问,为什么?
//question 1
//This method can be accessed directly when it is acquired. Why?
let varis = {
    int:a1=2,
    int:b1=3,
    float:c1 = 4.6,
    string:d1="i am string d1",
    dasdsad:e1="i am string el"
};
console.log(a1);//2
console.log(b1);//3
console.log(c1);//4.6
console.log(d1);//i am string d1
console.log(e1);//i am string e1
//问题二
//这种方式获取对象里的值还是普通的对象方式
//question 2
//This way gets the value in the object use the normal object mode
//
let varis = {
    a1:int=2,
    b1:int=3,
    c1:float = 4.6,
    d1:string="i am string d1",
    e1:eqwedsad="i am string el"
};
console.log(a1);//2
console.log(b1);//3
console.log(c1);//4.6
console.log(d1);//i am string d1
console.log(e1);//i am string e1
阅读 2.9k
4 个回答
let varis = {
    int:a1=2,
    int:b1=3,
    float:c1 = 4.6,
    string:d1="i am string d1",
    dasdsad:e1="i am string el"
};

以上代码其实就是先对a1等赋值,然后将其赋值表达式的返回值再传递给int等varis的key。
这样错是不会有错。。就是可能会让读你代码的人一脸懵逼-。-

就是普通的对象而已。不推荐写这种代码,会被打的。也不建议初学者看这些内容,会误人子弟的。

这个也就是

a = 1;
var obj = {
    int: a;
}

注意这里也就是a不使用 var,一般中生产环境开发都是使用严格模式,这样会报错,不建议使用

如果不使用严格模式,这个可以运行,但是会污染全局变量,也不建议使用

新手上路,请多包涵

初看吓一跳,还以为是什么稀有的语法。然后才反应过来就是普通的对象赋值.

let varis = {
    int:a1=2
};
console.log(a1);//2

在js里面int,float这种不是关键字,是可以当成变量名随意使用的。所以只是一个key为"int”的object。
这段语句先执行a1=2,因为a1没有定义默认就是全局变量。然后将这个赋值语句的返回值(就是2)再赋值给varis.int。

至于问题2直接就是报错了,a1,b1都没定义,不存在

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题