JavaScript的对象分类

为什么对象要分类?

举例

//正方形:边长、面积、周长
let zhengfangxing = {
    witdh:5,
    getArea(){
        return this.witdh * this.witdh;
    },
    getZhouChang(){
        return this.witdh * 4;
    }
}
//新手写法,多个正方形
let zhengfangxing2 = {
    witdh:4,
    getArea(){
        return this.witdh * this.witdh;
    },
    getZhouChang(){
        return this.witdh * 4;
    }
}
//...
//代码优化 for循环,宽度固定为4
let zhengfangxing = [];
for(let i = 0; i < 5; i++){
    zhengfangxing[i] = {
        witdh:4,
        getArea(){
            return this.witdh * this.witdh;
        },
        getZhouChang(){
            return this.witdh * 4;
        }
    }
}
//继续代码优化,浪费内存
let zfx = [];
let widthList = [5,6,5,6,5];
for(let i = 0; i < 5; i++){
    zfx[i] = {
        witdh:widthList[i],
        getArea(){
            return this.witdh * this.witdh;
        },
        getZhouChang(){
            return this.witdh * 4;
        }
    }
}

内存图如下:
在这里插入图片描述从内存图中,可以发现10个函数重复

//优化代码,借助原型,有一定优化,但是代码分散
let zxf = [];
let widthList = [5,6,5,6,5];
let zxfPrototype = {
    getArea(){
        return this.witdh * this.witdh;
    },
    getZhouChang(){
        return this.witdh * 4;
    }
}
for(let i = 0; i < 5; i++){
    zfx[i] = Object.create(zxfPrototype);
    zfx[i].witdh = widthList[i];
}
//继续优化代码 抽离到函数
let zfx = [];
let widthList = [5,6,5,6,5];
let zxfPrototype = {
    getArea(){
        return this.witdh * this.witdh;
    },
    getZhouChang(){
        return this.witdh * 4;
    }
}
function createZfx(width){
    let obj = Object.create(zxfPrototype);
    obj.width = width;
    return obj;
}
for(let i = 0; i < 5; i++){
    zfx[i] = createZfx( widthList[i]);
}
//继续代码优化,上面原型和抽离函数还是分散
let zfx = [];
let widthList = [5,6,5,6,5];
function createZfx(width){
    let obj = Object.create(zxfPrototype);
    obj.width = width;
    return obj;
}
createZfx.zxfPrototype = {
    getArea(){
        return this.witdh * this.witdh;
    },
    getZhouChang(){
        return this.witdh * 4;
    },
    constructor:createZfx
}
for(let i = 0; i < 5; i++){
    zfx[i] = createZfx( widthList[i]);
}
//最后优化,函数和原型结合,用new。(重写)
let zfxList = [];
let widthList = [5,6,5,6,5];
function zfx(witdh){
    this.witdh = width;
}
zfx.prototype.getArea = function(){
    return this.witdh * this.witdh;
}
zfx.prototype.getZhouChang = function(){
    return this.witdh * 4;
}
for(let i = 0; i < 5; i++){
    zfxList[i] = new zfx(widthList[i]);
}
总结
  1. new Obj()自动做了四件事
  • 自动创建空对象
  • 自动为空对象关联原型,原型地址指定为Obj.prototype
  • 自动将空对象作为this关键字运行构造函数
  • 自动返回this
  1. 构造函数 Obj
  • Obj函数本身负责给对象本身添加属性
  • Obj.prototype对象负责保存对象的共有属性
JS里面唯一的一个公式
  1. let Obj = new Object()的原型是Object.prototype
  2. let arr = new Array()的原型是Array.prototype
  3. let zfx = new zfx()的原型是zfx.prototype
  4. let fn = new Function()的原型是Function.prototype
  5. 因此:你是谁构造的,你的原型就是谁的prototype属性对应的对象
  6. 原型公式:对象.proto===其构造函数.prototype
  7. JS 中 proto 和 prototype 存在的意义是什么?
通过以上代码和总结可以知道构造函数、prototype、new
对象分类的理由
  1. 有很多对象拥有一样的属性和行为
  2. 需要把它们归为同一类
  3. 但也有很多对象拥有其他的属性和行为
  4. 所以需要不同的分类
  5. Object创建出来的对象,是最没有特点的对象
class语法引入
class Square{
    static x = 1;
    width = 0;
    constructor(width){
        this.width = this.width;
    }
    getArea(){
        return this.width * this.width;
    }
    getZhouChang(){
        return this.width * 4;
    }
    get area2(){
        return this.width * this.width;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaobangsky

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值