构造函数及实例化原理
function Car () {this.color = 'red'}console,log(Car) // 没有意义 不执行是不存在的Car() // 这个时候相当于 window.car() 函数体内是window.color = 'red'
function Car (color,brand){this.color = colorthis.brand = brandreturn this}let car1 = new Car ('red','Benz')
其实 Car () 执行 是吧window.Car() 执行了在GO里面
AO 保存color brand
但是有一个new 之后 会在 AO 里面生成一个this对象
变成
AO:{this:{}}
最后function (){} 会隐式 return this
如果构造函数的返回值是原始值的话 返回this ,如果是引用值是可以的
function Car (color){this.color = colorreturn 123}let car = new Car (‘red’)console,log(car) // red
function Car (color){this.color = colorreturn {}}let car = new Car (‘red’)console,log(car) // {}
包装类
new Number new Boolean new String 都有这样的特性
let a = 1let aa = new Number(1)aa.name = 'aa' //let b = aa + 1 // 2 失去了包装类

原始值没有方法和属性,不能使用 a.b 这样赋值,但是强行这样写 会 根据类型 new Number () / new Boolean () / new String () 会有一个 new Number(123).len = 3 的过程 ,但是构造函数返回的结果没有变量保存,就会delete 掉
var a = 123a.len = 3 js做了 new Number(123).len = 3 deleteconsolo.log(a.len) // undefined =======> 但是没有保存下来var b = '123'consolo.log(a.length) // 3
字符串.length 就是通过包装类来
let arr =[1,2,3,4,5]arr.length = 3 // arr = [1,2,3]let arrSec = [1,2,3,4,5]arrSec.length = 6 // arrSec = [1,2,3,4,5,empty]let str = 'abc'str.length = 1console.log(str) // 'abc'//-----------------------------------------------str.length => new String(str).lengthstr.length = 1 => new String(str).length = 1 delete
配合 js 得一个函数 string.charCodeAt(0 /‘’位数’/)
function getS(str) {let num = 0for (let i = 0; i < str.length; i++) {console.log(str.charCodeAt(i));if (str.charCodeAt(i) > 255) {num++}}console.log(num);return str.length + num}console.log(getS('你好朋友'));
