对象属性值得类型:
属性值的类型:基本值,函数,对象
对象类型的属性值
对象的属性值是一个基本值
<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>