原型的指向是否可以改变 原型最终指向了哪里 原型指向改变如何添加方法和访问
生活随笔
收集整理的這篇文章主要介紹了
原型的指向是否可以改变 原型最终指向了哪里 原型指向改变如何添加方法和访问
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
原型的指向是否可以改變
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// function Student(){// }// Student.prototype.study = function(){// console.log("就是天天學(xué)習(xí)");// };// Student.prototype = {// eat:function(){// console.log("哈哈,好吃的榴蓮");// }// };// var stu = new Student();// stu.eat();// stu.study();// 人的構(gòu)造函數(shù)function Person(age){this.age = 10;}// 人的原型對象方法Person.prototype.eat = function(){console.log("人的吃");};// 學(xué)生的構(gòu)造函數(shù)function Student(){}Student.prototype.sayHi = function(){console.log("小蘇你好帥哦");};// 學(xué)生的原型,指向了一個人的實例對象Student.prototype = new Person(10);var stu = new Student();stu.eat();// stu.say();// 原型指向可以改變// 實例對象的原型__proto__指向的是該對象所在的構(gòu)造函數(shù)的原型對象// 實例對象的原型__proto__// 構(gòu)造函數(shù)的原型(prototype)如果改變了,實例對象的原型(__proto__)指向// 也會發(fā)生改變// 原型的指向也是可以改變的// 實例對象和原型對象之間的關(guān)系是通過__proto__原型來聯(lián)系起來的,這個關(guān)系就是// 原型鏈</script> </head> <body></body> </html>原型最終指向了哪里
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>function Person(){}Person.prototype.eat = function(){console.log("吃東西");};var per = new Person();// console.dir(per);// console.dir(Person);// 實例對象中有__proto__原型// 構(gòu)造函數(shù)中有prototype原型// prototype是對象// 所以,prototype這個對象中也有__proto__,那么指向了哪里// 實例對象中的__proto__指向的是構(gòu)造函數(shù)的prototype// 所以,prototype的這個對象中的__proto__指向的應(yīng)該是某個構(gòu)造函數(shù)的原型// prototype// Person的prototype中的__proto__的指向// console.log(Person.prototype.__proto__);// 實例對象__proto__---->Person.prototype的__proto__---->Object.prototype// 的__proto__是nullconsole.log(per.__proto__==Person.prototype);console.log(per.__proto__.__proto__==Person.prototype.__proto__);console.log(Person.prototype.__proto__==Object.prototype);console.log(Object.prototype.__proto__);</script> </head> <body></body> </html>原型指向改變?nèi)绾翁砑臃椒ê驮L問
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>Document</title><script>// // 人的構(gòu)造函數(shù)// function Person(age){// this.age = age;// }// // 人的原型中添加方法// Person.prototype.eat = function(){// console.log("人正在吃東西");// };// // 學(xué)生構(gòu)造函數(shù)// function Student(sex){// this.sex = sex;// };// // 改變了原型對象的指向// Student.prototype = new Person(10);// // 學(xué)生的原型中添加方法----現(xiàn)在原型中添加方法// Student.prototype.sayHi = function(){// console.log("您好");// };// var stu = new Student("男");// stu.sayHi();// stu.eat();// // 人的構(gòu)造函數(shù)// function Person(age){// this.age = age;// }// // 人的原型中添加方法// Person.prototype.eat = function(){// console.log("人正在吃東西");// };// // 學(xué)生構(gòu)造函數(shù)// function Student(sex){// this.sex = sex;// };// // 改變了原型對象的指向// Student.prototype = new Person(10);// // 學(xué)生的原型中添加方法----現(xiàn)在原型中添加方法// Student.prototype.sayHi = function(){// console.log("您好");// };// var stu = new Student("男");// // stu.sayHi();// // stu.eat();// console.dir(stu);// 如果原型指向改變了,那么就應(yīng)該在原型改變指向之后添加原型方法function Person(age){this.age = age;}// 指向改變了Person.prototype = {eat:function(){console.log("吃");}};// 先添加原型方法Person.prototype.sayHi = function(){console.log("您好");};var per = new Person(10);per.sayHi();</script> </head> <body></body> </html>?
總結(jié)
以上是生活随笔為你收集整理的原型的指向是否可以改变 原型最终指向了哪里 原型指向改变如何添加方法和访问的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 把随机数对象暴露给window成为全局对
- 下一篇: 实例对象的属性和原型对象中的属性重名问题