如果对象键中包含int,那么JavaScript中的值必须是整数,我们能限制它吗?
let myData {
dob_int: 1990
}
myData.dob_int = "a"; //need to be throw error发布于 2022-08-10 16:48:26
你可以创造一个这样的策划人。如果值正确,调用setter设置'dob_Int‘。
let myData = {
set dobInt(value){
if(!isNaN(value)){
console.log('Is Valid')
this.dob_int = value
} else {
console.log('Is not Valid')
}
}
}
myData.dobInt = 'A'
//will log 'Is not Valid' and will not update the object key value
myData.dobInt = 5
//will log 'Is Valid' and will set dob_int to the valuehttps://stackoverflow.com/questions/73309422
复制相似问题