脚本宝典收集整理的这篇文章主要介绍了js实例教程-问题讨论:js原型的指向可以是否可以发生改变?,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。小宝典致力于为广大程序猿(媛)提供高品质的代码服务,请大家多多光顾小站,小宝典在此谢过。
<!DOCTYPE htML> <html lang="en"> <head> <meta charset="UTF-8"> <tITle>title</title> <script> // 构造函数中的this就是实例对象 // 原型对象中方法中的this就是实例对象 // function Person(age) { // this.age=age; // console.LOG(this); // } // Person.PRototype.eat=function () { // console.log(this); // console.log("您吃了没,走着,吃点臭豆腐去"); // }; // VAR per=new Person(10); // per.eat(); // console.log(per); // function Student() { // // } // Student.prototype.study=function () { // console.log("就是天天学习,学习如何做人,如何敲代码,如何成为人"); // }; // Student.prototype={ // eat:function () { // console.log("哈哈,好吃的榴莲酥"); // } // }; // // var stu=new Student(); // // stu.eat(); //人的构造函数 function Person(age) { this.age=10; } //人的原型对象方法 Person.prototype.eat=function () { console.log("人的吃"); }; //学生的构造函数 function Student() { } Student.prototype.sayHi=function () { console.log("嗨,小苏你好帅哦"); }; //学生的原型,指向了一个人的实例对象 Student.prototype=new Person(10); var stu=new Student(); stu.eat(); stu.sayHi(); //原型指向可以改变 //实例对象的原型__proto__指向的是该对象所在的构造函数的原型对象 //构造函数的原型对象(prototype)指向如果改变了,实例对象的原型(__proto__)指向也会发生改变 //原型的指向是可以改变的 //实例对象和原型对象之间的关系是通过__proto__原型来联系起来的,这个关系就是原型链 </script> </head> <body> </body> </html>
问题讨论:js原型的指向可以是否可以发生改变?
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>title</title> <script> // 构造函数中的this就是实例对象 // 原型对象中方法中的this就是实例对象 // function Person(age) { // this.age=age; // console.log(this); // } // Person.prototype.eat=function () { // console.log(this); // console.log("您吃了没,走着,吃点臭豆腐去"); // }; // var per=new Person(10); // per.eat(); // console.log(per); // function Student() { // // } // Student.prototype.study=function () { // console.log("就是天天学习,学习如何做人,如何敲代码,如何成为人"); // }; // Student.prototype={ // eat:function () { // console.log("哈哈,好吃的榴莲酥"); // } // }; // // var stu=new Student(); // // stu.eat(); //人的构造函数 function Person(age) { this.age=10; } //人的原型对象方法 Person.prototype.eat=function () { console.log("人的吃"); }; //学生的构造函数 function Student() { } Student.prototype.sayHi=function () { console.log("嗨,小苏你好帅哦"); }; //学生的原型,指向了一个人的实例对象 Student.prototype=new Person(10); var stu=new Student(); stu.eat(); stu.sayHi(); //原型指向可以改变 //实例对象的原型__proto__指向的是该对象所在的构造函数的原型对象 //构造函数的原型对象(prototype)指向如果改变了,实例对象的原型(__proto__)指向也会发生改变 //原型的指向是可以改变的 //实例对象和原型对象之间的关系是通过__proto__原型来联系起来的,这个关系就是原型链 </script> </head> <body> </body> </html>
觉得可用,就经常来吧!Javascript技巧 脚本宝典 欢迎评论哦! js技巧,巧夺天工,精雕玉琢。小宝典献丑了!
以上是脚本宝典为你收集整理的js实例教程-问题讨论:js原型的指向可以是否可以发生改变?全部内容,希望文章能够帮你解决js实例教程-问题讨论:js原型的指向可以是否可以发生改变?所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。