构造函数内部原理
- 在函数体最前面隐式得加上this = {}
- 执行 this.xxx = xxx;
- 隐式的返回this
包装类
定义一个原始数字或字符串,在JS里面,其实有两种方法
- var num = 123; var str = 'abcd';
- var num = new number(123); var str = new string('abcd');
但是这两种方法所定义出来的是有区别的。第二种其实是用构造函数构造出来的,是对象,但是可以直接使用。
第一种其实是经过了包装类的过程,即如下
var str = "abcd";str.length = 2;//new String('abcd').length = 2; delete
JavaScript里有三种类型是会经过包装类的
- new String();
- new Boolean();
- new Number();