猿问

length为0或为空

在表单中,value.length = '',和value.length = 0;有什么区别吗?

慕容3067478
浏览 635回答 1
1回答

繁星点点滴滴

value 是什么啊?value 只接收 string 类型的值,所以 value=0 时会把 0 转换为字符串 '0',然后赋值。最终表单的内容是 0。至于 length 属性:字符串对 length 赋值,不改变原始串。如果是数组的话,赋值为 0 会清空数组,但是字符串不会。数组的 length 属性是只读的,在严格模式下,对 length 属性赋值会报类型错误TypeError:'use strict';var num = 'aaaaa';console.log(num.length);num.length = 0;console.log(num);console.log(num.length);会出错:Uncaught TypeError: Cannot assign to read only property 'length' of string 'aaaaa'
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答