Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[js] 第599天 请解释下为什么下面第一种方式会报错而第二种不会? #3269

Open
haizhilin2013 opened this issue Dec 4, 2020 · 1 comment
Labels
js JavaScript

Comments

@haizhilin2013
Copy link
Collaborator

第599天 请解释下为什么下面第一种方式会报错而第二种不会?

3+1官网

我也要出题

// 第一种:构造函数创建
var object=new Object();
object.a=1;
object.8=8; //Unexpected number

// 第二种:字面量创建
var object = {
  a: 1,
  8: 2
};

@haizhilin2013 haizhilin2013 added the js JavaScript label Dec 4, 2020
@ReginaLiang
Copy link

object的键会被自动转为字符串。
但是obj.name 属性名只能是字符串,不能是数字, 因为会被当成小数点。
第一种可以使用object[8] = 8;
第二种会被转成字符串所以不会报错。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
js JavaScript
Projects
None yet
Development

No branches or pull requests

2 participants