loading...
loading...
<script> //引用类型中都有prototype原型,该属性指向对象 var obj={ country:"中国", name:"王五" }; 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(stu.__proto__.__proto__);//得到obj的原型对象 console.log(obj.__proto__);//得到obj的原型对象 console.log(obj.__proto__.__proto__);//null 得到obj的原型对象的原型对象 //判断一个对象是否指向了构造函数的原型对象 console.log(Student.prototype.isPrototypeOf(stu));//true console.log(stu.name);//stu对象中的name 张三 优先使用Student的 如果没有使用原型中的name console.log(stu.hasOwnProperty("name"));//true 判断是否有自己本身的name属性 console.log("name" in stu );//true 只要stu本身 或者原型都有name属性 就返回true </script>
发表评论: