简体   繁体   中英

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

this stackoverflow question and answer

It basically says you're allowed to use keywords as object property names, since ES5. Also, your property will remain unchanged until you change it somehow, so yes, it serves as a default value for that key.

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