原型设计
<script> //引用类型中都有prototype原型,该属性指向对象 var obj={ country:"中国" }; function Student(name,age) { this.name=name; this.age=age; this.study= function () { console.log("我叫"+this.name+",今年"+this.age+"岁,我爱学习。"); }; } Student.prototype=obj; var stu=new Student("张三","22");//内部创建了一个__proto__ 都指向了prototype var stu2=new Student("李四","22");//内部创建了一个__proto__ 都指向了prototype // console.log(stu2.country) console.log(Student.prototype);//Object {country: "中国"} console.log(stu.__proto__);//Object {country: "中国"} </script>
发表评论: