js中的构造函数

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>dog</title>
<script>
function Dog(name, breed, weight){
this.name = name;
this.breed = breed;
this.weight = weight;
this.bark = function(){
if (this.weight > 25){
alert(this.name = " says Woof!");
} else {
alert(this.name = " says Yip!");
}
};
} var fido = new Dog("Fido", "Mixed", 38);
var fluffy = new Dog("Fluffy", "Poodle", 30);
var spot = new Dog("Spot", "Chihuahua", 10);
var dogs = [fido, fluffy, spot]; for (var i = 0; i < dogs.length; i++){
dogs[i].bark()
} //判断对象是否是构造函数的实例
if (fido instanceof Dog){
alert("没错");
} //增删对象的独特属性和方法
fido.owner = "Bob";
delete fido.weight; fido.trust = function(person){
return (person === "Bob");
} </script>
</head>
<body> </body>
</html>
上一篇:gvim e303 无法打开 “[未命名]“的交换文件,恢复将不可能


下一篇:mysql 乱码问题