在Function对象上扩展method方法
2016-05-26 17:12
411 查看
;(function() { /** * 在Function对象上扩展method方法 * @param {String} name 扩展的方法名称 * @param {Function} callback 方法的定义 * @return {Function} return this 提供链式调用 */ if(typeof Function.prototype.method !== 'function'){ Function.prototype.method = function(name, callback) { // 在当前function上扩展name方法 this.prototype[name] = callback; // 便于method方法的链式调用 return this } } // method方法的运用 var Hoogle = function() { this.name = "Hoogle", this.major = "F2E" }; // 扩展hoogle构造函数上的原型方法 Hoogle.method("getName", function() { return this.name }).method("setName", function(newname) { this.name = newname; return this }).method("getMajor", function() { return this.major }).method("setMajor", function(newmajor) { this.major = newmajor; return this }); // 实例化Hoogle var hoogle = new Hoogle(); // 设置静态属性并输出结果 hoogle.setName("newHoogle").setMajor("newMajor"); console.info(hoogle.getName(), hoogle.getMajor()); })();
相关文章推荐
- 菜鸟对使用AIDL的一点理解
- android native 内存泄露检查(libc.debug.malloc )
- Ext JS 6开发实例(三) :主界面设计
- Ext JS 6开发实例(三) :主界面设计
- Lombok 安装、入门 - 消除冗长的 java 代码
- Ubuntu14连接MySql报错“can't connect to local mysql server through socket '/var/run/mysqld/mysqld.sock'”
- android studio很好的一个JNI 入门博文
- ps制作爆碎感的玻璃海报特效
- MyBatis学习 之 三、动态SQL语句
- HDU 4405 Aeroplane chess
- make与gmake的区别
- CentOS配置SVN服务器
- leetcode 145. Binary Tree Postorder Traversal-后续遍历|递归|非递归
- 同时也希望通过分享来增
- 同时也希望通过分享来增
- BootStrap中的table实现数据填充与分页应用小结
- 想就着这个机会
- Ext JS 6开发实例(三) :主界面设计
- Linux命令 - Find
- 基于linux 3.10.49内核的gpio流程分析