JS 对象分类
比如下面的代码
let squareList = []
let widthList = [5,6,5,6,5,6,5,6]
for(let i = 0; i<8; i++){
squareList[i] = {
width: widthList[i],
getArea(){
return this.width * this.width
},
getLength(){
return this.width * 4
}
}
}
上面的代码,虽然得到了8个正方形的面积和周长,但是这段代码会浪费内存,那么如何对这段代码进行优化?
借助原型,将这8个对象的共用属性放进原型里
let squareList = []
let widthList = [5,6,5,6,5,6,5,6]
let squarePrototype = {
get Area(){