javascript面向对象 学习笔记(一)对象

1.对象的创建

1.1 对象创建语法

const circle = {
    radius:1,
    location:{
        x:1,
        y:1
    },
    draw:function(){
        console.log("draw");
    }
};

1.2 使用工厂函数创建对象

function createCircle(radius){
    return {
        radius,
        location:{
            x:1,
            y:1
        },
        draw:function(){
            console.log("draw");
        }
    };
}

let circle = createCircle(1);
circle.draw();

1.3 使用构造函数创建对象

function Circle(radius){
    this.radius = radius;
    this.draw = function(){
        console.log("draw");
    }
}

const another = new Circle(1);
another.draw();

每个javascript中的对象都有一个叫构造函数的属性,它是一个用于创建这个对象的构造方法的引用。
在这里插入图片描述上图中,circle对象是使用工厂函数创建的,它调用了javascript的内建函数。当我们使用创建对象语法来创建对象时,javascript内部也使用了这个构造函数。

let x = {};

上面这行代码被javascript解释为:

let x = new Object();

javascript中其他的一些构造函数:

new String();
new Boolean();
new Number();

2.javascript中的值类型和引用类型

值类型:Number,String,Boolean,Symbol,undefined,null
引用类型:Object,Function,Array

3.添加或移除属性

javascript中的对象是动态的,这意味着在对象创建后我们可以随时添加或移除一些属性。

3.1 添加属性

function Circle(radius){
    this.radius = radius;
    this.draw = function(){
        console.log("draw");
    }
}

const circle = new Circle(1);

circle.location = {x:1,y:2};
circle['location'] = {x:1,y:3};

3.2 删除属性

delete circle.location;
delete circle['location'];

4.私有属性和方法

function Circle(radius){
    this.radius = radius;
    this.draw = function(){
        console.log("draw");
    }
}

const another = new Circle(1);
another.draw();

像这样定义的对象,this指向新的circle对象。如果我们把属性声明为函数的内部变量,当我们离开这个函数的时候,这个变量也就离开了其作用域,被销毁了。
我们可以利用上述特性,隐藏某些成员。

function Circle(radius){
    this.radius = radius;
    let defaultLocation = {x:0,y:0};

    let computeOptimumLocation = function(factor){
        console.log(factor);
    }

    this.draw = function(){
        computeOptimumLocation(0.1);
        console.log("draw");
    }
}

const circle = new Circle(10);
circle.draw();

像上面这样的写法,我们把defaultLocation和computeOptimumLocation设置为circle的私有成员,但准确来说,它们不是circle的私有成员,因为在技术上它们不在circle对象的内部,它们是Circle函数内部的局部变量,但从面对对象的角度,我们可以称他们为circle对象的私有成员。

5.Getters and Setters

function Circle(radius){
    this.radius = radius;
    let defaultLocation = {x:0,y:0};

    this.getDefaultLocation = function(){
        return defaultLocation;
    };
    this.setDefaultLocation = function(x,y){
        defaultLocation.x=x;
        defaultLocation.y=y;
    };

    this.draw = function(){
        console.log("draw");
    };

    Object.defineProperty(this,'defaultLocation',{
        get:function(){
            return defaultLocation;
        },
        set:function(value){
            if(!value.x || !value.y){
                throw new Error('不合法的地址');
            }
            defaultLocation = value;
        }
    });
}

const circle = new Circle(10);
circle.defaultLocation = {x:1,y:1};

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值