手机
当前位置:查字典教程网 >编程开发 >Javascript教程 >JS自定义混合Mixin函数示例
JS自定义混合Mixin函数示例
摘要:本文实例讲述了JS自定义混合Mixin函数。分享给大家供大家参考,具体如下:/*增加函数*/functionaugment(receivin...

本文实例讲述了JS自定义混合Mixin函数。分享给大家供大家参考,具体如下:

<script type="text/javascript"> /* 增加函数 */ function augment(receivingClass, givingClass) { for(methodName in givingClass.prototype) { if(!receivingClass.prototype[methodName]) { receivingClass.prototype[methodName] = givingClass.prototype[methodName]; } } } /* 改进的增加函数 */ function augment(receivingClass, givingClass) { if(arguments[2]) { // Only give certain methods. for(var i = 2, len = arguments.length; i < len; i++) { receivingClass.prototype[arguments[i]] = givingClass.prototype[arguments[i]]; } } else { // Give all methods. for(methodName in givingClass.prototype) { if(!receivingClass.prototype[methodName]) { receivingClass.prototype[methodName] = givingClass.prototype[methodName]; } } } } var Author = function Author(name, books) { // 构造函数 this.name = name; this.books = books || 'default value'; }; Author.prototype = { getName: function() { return this.name; }, getBooks: function() { return this.books; } }; var Editor = function Editor() { }; Editor.prototype = { hello: function() { return 'Hello,'+this.name; } }; augment(Author, Editor); var author = new Author('Ross Harmes', ['JavaScript Design Patterns']); console.log(author.getName()); console.log(author.getBooks()); console.log(author.hello()); </script>

结果:

JS自定义混合Mixin函数示例1

经过拼接处理之后,author就获取到了hello方法了,属性还是自己的name。

希望本文所述对大家JavaScript程序设计有所帮助。

【JS自定义混合Mixin函数示例】相关文章:

自动设为主页

JQuery勾选指定name的复选框集合并显示的方法

JS中字符串trim()使用示例

JavaScipt中Function()函数的使用教程

浅析javascript函数表达式

总结一些js自定义的函数

JavaScript实现广告的关闭与显示效果实例

JS制作手机端自适应缩放显示

javaScript中with函数用法实例分析

JavaScript中eval函数的问题

精品推荐
分类导航