谈一谈javascript中继承的多种方式(2)

<script> +(function() { /*继承的固定函数*/ function inheritPrototype(subType, superType) { var prototype = Object(superType.prototype); prototype.constructor = subType; subType.prototype = prototype; } function Person(name) { this.name = name; } Person.prototype.say = function() {} function Student(name, age) { Person.call(this, name); this.age = age; } inheritPrototype(Student, Person); var xiaozhang = new Student('小张', 20); console.log(xiaozhang.name) })() </script>

8、使用第三方框架实现继承

<script src='https://www.jb51.net/simple.js'></script> <!-- 使用的第三方框架simple.js --> <script> +(function() { < script > var Person = Class.extend({ init: function(age, name) { this.age = age; this.name = name; }, ppp: function() { alert("你懂的"); } }); var Man = Person.extend({ init: function(age, name, height) { this._super(age, name); this.height = height; }, ppp: function() { /*调用父类的同名方法*/ this._super(); /*同时又可以调用自己的方法*/ alert("好害羞 -,- "); } }); var xiaozhang = new Man(21, '小张', '121'); xiaozhang.ppp(); })()

希望对大家学习javascript程序设计有所帮助。

您可能感兴趣的文章:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wgwyyy.html