美文网首页
尽量少用new创建基本类型

尽量少用new创建基本类型

作者: 鲲鹏f2e | 来源:发表于2018-06-22 12:27 被阅读0次

为什么要尽量少使用new来创建基本类型呢
举个🌰:

var string = 'cloth';
string.color = 'red'
console.log(string.color) // undefined

因为new 但是用new创建的时候你会发现

var string = new String('cloth');
string.color = 'red' // red

这样在使用上回造成混淆,使用

typeof string //obj

会发现是一个对象

相关文章

网友评论

      本文标题:尽量少用new创建基本类型

      本文链接:https://www.haomeiwen.com/subject/ealkyftx.html