简体   繁体   English

在 JavaScript 对象中设置默认值?

[英]Set Default in JavaScript Object?

Would this work say a new user registers for the site and I want the default role to be "user"?这项工作是否会说新用户注册了该站点并且我希望默认角色为“用户”?

I want that when a person signs up, the default role is set to "user", and be able to change it later to, let's say "admin".我希望当一个人注册时,默认角色设置为“用户”,并且可以稍后将其更改为“管理员”。

  firstName: {
type: String,
required: true
},
lastName: {
type: String,
required: true
},
dob: {
type: Date,
required: true
},
email: {
type: String,
required: true
},
username: {
type: String,
required: true
},
password: {
type: String,
required: true
},
role: {
type: String,
default: 
}
});

这是某种Schema (猫鼬),如果是,那么是的,它会起作用。

default

Is a keyword in JavaScript, so check out是 JavaScript 中的关键字,请查看

this stackoverflow question and answer这个stackoverflow问答

It basically says you're allowed to use keywords as object property names, since ES5.它基本上说你可以使用关键字作为对象属性名称,因为 ES5。 Also, your property will remain unchanged until you change it somehow, so yes, it serves as a default value for that key.此外,您的属性将保持不变,直到您以某种方式更改它为止,所以是的,它用作该键的默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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