<!DOCTYPE HTML>
<html>
<head>
<style></style>
</head>
<body>
<p>js对象</p>
<script>
// 对象初始化器
var car = {type:"porsche",
model:"911",
color:"white"
};
// 1、通过创建一个构造函数来定义对象的类型。首字母大写是非常普遍而且很恰当的惯用法。
// 2、通过 new 创建对象实例。
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
var mycar = new Car("Eagle", "Talon TSi", 1993);
// 添加新属性
mycar.color="black";
// Object.create()方法
var Animal = {
type: "Invertebrates", // 属性默认值
displayType : function() { // 用于显示 type 属性的方法
console.log(this.type);
}
}
// 创建一种新的动物——animal1
var animal1 = Object.create(Animal);
animal1.displayType(); // Output:Invertebrates
// 创建一种新的动物——Fishes
var fish = Object.create(Animal);
fish.type = "Fishes";
fish.displayType(); // Output:Fishes
</script>
</body>
</html>