码迷,mamicode.com
首页 > 其他好文 > 详细

for..in loops iterate over the entire prototype chain, which is virtually never what you want.

时间:2021-04-27 14:24:47      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:you   bsp   span   rgba   rgb   form   data   reac   virt   

for..in loops iterate over the entire prototype chain, which is virtually never what you want. 

意思是使用for..in会遍历整个原型链,这样不是很好的实现方法,推荐使用Object.keys

formRules: {
  name: true,
  cardType: true,
  certificateNo: true
 },
 formData: {
  name: ‘‘,
  certificateType: ‘‘,
  certificateNo: ‘‘
 }

原代码

for (const key in this.formData) {
     if (!this.formData[key]) { this.formRules[key] = false; valid = false }
     if (this.formData[key]) this.formRules[key] = true
}

修改后的代码

Object.keys(this.formData).forEach(key => {
 if (!this.formData[key]) { this.formRules[key] = false; valid = false }
 if (this.formData[key]) this.formRules[key] = true
})

 

for..in loops iterate over the entire prototype chain, which is virtually never what you want.

标签:you   bsp   span   rgba   rgb   form   data   reac   virt   

原文地址:https://www.cnblogs.com/qianxiaoPro/p/14703064.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!