JavaScript之对象属性值得类型

对象属性值得类型:
属性值的类型:基本值,函数,对象
对象类型的属性值

对象的属性值是一个基本值

<script type="text/javascript">
        //方式一
        function Student(name, age, sex, html, css, javascript) {
            this.name = name;
            this.age = age;
            this.sex = sex;
            this.html = html;
            this.css = css;
            this.javascript = javascript;
            this.show = function () {
                console.log("我是一个学生,信息如下:");
                console.log(this.name + "," + this.age + "," + this.sex);
                console.log(this.html + "," + this.css + "," + this.javascript);
            };
        }
        var stu1 = new Student("唐伯虎", 20, "男", 92, 86, 100);
        stu1.show();

        //方式二:改良版,可以用一个数组替换多个属性
        function Student(name, age, sex, scores) {
            this.name = name; //基本值
            this.age = age;
            this.sex = sex;
            this.scores = scores; //对象
            this.show = function () { //函数
                console.log("我是一个学生,信息如下:");
                console.log(this.name + "," + this.age + "," + this.sex);
                console.log(this.scores.html + "," + this.scores.css + "," + this.scores.javascript);
            };
        }
        var scores = {
            html: 92,
            css: 86,
            javascript: 99
        };
        var stu2 = new Student("唐伯虎", 20, "男", scores);
        stu2.show();
    </script>

对象的属性值是一个对象

<script type="text/javascript">
    function Address(province, city, district, street) {
        this.province = province;
        this.city = city;
        this.district = district;
        this.street = street;
    }

    function Customer(name, phone, address, pets) {
        this.name = name;
        this.phone = phone;
        this.address = address;
        this.pets = pets; //添加一个属性pets,表示客户领养的宠物,可以有多个
        this.show = function () {
            console.log(this.name + "," + this.phone + "," + this.address.city);
            console.log("领养的宠物信息如下:");
            for (var key in pets) {
                var pet = pets[key];
                console.log(pet.name + "," + pet.type + "," + pet.age);
            }
        };
    }
    var c1 = new Customer("ysy", "025-88888888", new Address("xx省", "xx市", "xx区", "xx街"));
    c1.show();
</script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值