<!Doctype html> <html xmlns="http://www./1999/xhtml"> <head> <title></title> <script type="text/javascript"> function Cat(name){ this.name=name; this.show=function(){ alert(this.name) } }
var c1=new Cat("大黄") var c2=new Cat("小黄") c1.show() c2.show() alert(c1.show==c2.show) // 每个对象都会进行存储 会浪费空间 //第二种方法 //javascript中的每个对象都有prototype属性,Javascript中对象的prototype属性的解释是:返回对象类型原型的引用。 function Scat(name){ this.name=name } Scat.prototype.type="猫科动物" Scat.prototype.show=function(){ // prototype alert(this.name) alert("33") } var d1=new Scat("大黄") //将会得到 prototype 的所有属性 var d2=new Scat("小黄") // d1.show() // d2.show() alert(d1.show==d2.show) alert(Scat.prototype.isPrototypeOf(d1)) //判断这个对象 是不是该继承对象 d1 是否继承了 Scat alert(d1.hasOwnProperty("name")) // 判断他是不是有这个属性 </script>
</head>
<body>
</body>
</html>
|