JavaScript中继承用法实例分析
更新时间:2015年05月16日 09:11:59 作者:不吃皮蛋
这篇文章主要介绍了JavaScript中继承用法,以实例形式较为详细的分析了javascript实现继承的相关技巧,需要的朋友可以参考下
本文实例分析了JavaScript中继承的用法。分享给大家供大家参考。具体如下:
// define the Person Class function Person() {} Person.prototype.walk = function(){ alert ('I am walking!'); }; Person.prototype.sayHello = function(){ alert ('hello'); }; // define the Student class function Student() { // Call the parent constructor Person.call(this); } // inherit Person Student.prototype = new Person(); // correct the constructor pointer because it points to Person Student.prototype.constructor = Student; // replace the sayHello method Student.prototype.sayHello = function(){ alert('hi, I am a student'); } // add sayGoodBye method Student.prototype.sayGoodBye = function(){ alert('goodBye'); } var student = new Student(); student.sayHello(); student.walk(); student.sayGoodBye(); // check inheritance alert(student instanceof Person); // true alert(student instanceof Student); // true
希望本文所述对大家的javascript程序设计有所帮助。
相关文章
DOM操作原生js 的bug,使用jQuery 可以消除的解决方法
下面小编就为大家带来一篇DOM操作原生js 的bug,使用jQuery 可以消除的解决方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-09-09javascript消除window.close()的提示窗口
有人问起,怎么去掉js调用window.close()时怎么去掉那可恶的提示,咋一看好像还真不好弄,IE的安全机制好像就不允许通过脚本关闭本页面,但是IE好像可以允许js关闭弹出窗口,那我们是不是可以通过一定的技巧欺骗一下IE,绕过去呢。鼓捣了几下,似乎还真可以做到2015-05-05
最新评论