HighCharts源码学习---扩展函数wrap和扩展对象 extendClass
2015-12-27 14:24
302 查看
Highcharts.wrap在原函数的基础上扩展原函数
Highcharts扩展原型对象
/** * Wrap a method with extended functionality, preserving the original function * @param {Object} obj The context object that the method belongs to * @param {String} method The name of the method to extend * @param {Function} func A wrapper function callback. This function is called with the same arguments * as the original function, except that the original function is unshifted and passed as the first * argument. */
var wrap = Highcharts.wrap = function (obj, method, func) { //备份原函数 var proceed = obj[method]; obj[method] = function() { //拷贝一份实参 var args = Array.prototype.slice.call(arguments); //将原函数的引用放到第一个入参 args.unshift(proceed); //保持上下文调用func func.call(this, args); }
}利用wrap重新封装parseInt()
wrap(window, parseInt, function(proceed, str, scale) { if(typeof str === 'number') return proceed.call(this, str); var hexChars = 'abcdef', decChars = '9'; var hasHex = false,hasDec = false,i = 0, len = str.length; if(!scale) { if(str.charAt(0) === '0') { if(str.charAt(1) === 'x') { return proceed.call(this, str, 16); } else { for(i = 0;i < len; i++) { var char = str.charAt(i).toLowerCase(); if(hexChars.indexOf(char) > -1) hasHex = true; if(decChars.indexOf(char) > -1) hasDec = true; } } if(hasHex) { return procced.call(this, str, 16); }else if(hasDec) { return procced.call(this, str, 10); } else { return procced.call(this, str, 8); } } return procced.call(this, str, 10); } })
Highcharts扩展原型对象
/** * Extend a prototyped class by new members * @param {Object} parent * @param {Object} members */ function extendClass(Parent, members) { var object = function () { }; object.prototype = new Parent(); extend(object.prototype, members); return object; }
相关文章推荐
- socket programming in kernel
- 【重走普及路】【分治】【经典算法】快速幂
- 文章标题
- git 的基本用法
- 贪吃蛇详解Windows编程(一)
- DP---求给定矩阵中最大正方形的维度
- DP---求给定矩阵中最大正方形的维度
- (浮点数及整数)高精度乘除法
- Android—— ubuntu下【CTS】測试TV真机
- 18.1 老式图元文件格式
- MYSQL查看和增加用户权限
- linux command - sort
- 为什么很多大学生不学车?
- thinkphp3.2实现微信JSAPI支付
- method="get/post",两种方式的区别
- DP---最长上升/下降子序列
- Canvas中的非零环绕规则原理
- 华中科技大学_2008___考研计算机_复试上机
- DP---最长上升/下降子序列
- hibernate基于注解的维护权反转:@OneToMany(mappedBy=)