写自已的js类库需要的核心代码


Posted in Javascript onJuly 16, 2012
(function(win) { 
var toString = Object.prototype.toString; 
var hasOwn = Object.prototype.hasOwnProperty; 
var class2type = {}; 
class2type["[object Boolean]"] = "boolean"; 
class2type["[object Number]"] = "number"; 
class2type["[object String]"] = "string"; 
class2type["[object Function]"] = "function"; 
class2type["[object Array]"] = "array"; 
class2type["[object Date]"] = "date"; 
class2type["[object RegExp]"] = "regexp"; 
class2type["[object Object]"] = "object"; 
win.type = function(obj) { 
return obj == null ? String(obj) : class2type[toString.call(obj)] || "object"; 
}; 
win.isBoolean = function(obj) { 
return type(obj) === "boolean"; 
}; 
win.isNumber = function(obj) { 
return type(obj) === "number"; 
}; 
win.isString = function(obj) { 
return type(obj) === "string"; 
}; 
win.isDate = function(obj) { 
return type(obj) === "date"; 
}; 
win.isRegExp = function(obj) { 
return type(obj) === "regexp"; 
}; 
win.isObject = function(obj) { 
return type(obj) === 'object'; 
}; 
win.isFunction = function(obj) { 
return type(obj) === "function"; 
}; 
win.isArray = function(obj) { 
return type(obj) === "array"; 
}; 
win.isWindow = function(obj) { 
return obj 
&& typeof obj === "object" 
&& "setInterval" in obj; 
}; 
win.isNumeric = function(obj) { 
return !isNaN(parseFloat(obj)) && isFinite(obj); 
}; 
win.isPlainObject = function(obj) { 
if (!obj 
|| type(obj) !== "object" 
|| obj.nodeType 
|| isWindow(obj)) { 
return false; 
} 
try { 
if (obj.constructor 
&& !hasOwn.call(obj, "constructor") 
&& !hasOwn.call(obj.constructor.prototype, "isPrototypeOf")) { 
return false; 
} 
} catch (e) { 
return false; 
} 
var key; 
for (key in obj) { 
} 
return key === undefined || hasOwn.call(obj, key); 
}; 
win.isEmptyObject = function(obj) { 
for ( var name in obj) { 
return false; 
} 
return true; 
}; 
win.isPrimitive = function(obj){ 
var type = typeof obj; 
return type === 'string' || type === 'number' || type === 'boolean'; 
}; 
//HTMLElement 
win.isElement = function(obj){ 
return obj ? obj.nodeType === 1 : false; 
}; 
//TextNode 
win.isTextNode = function(obj){ 
return obj ? obj.nodeName === "#text" : false; 
}; 
win.isIterable = function(obj){ 
return (obj && typeof obj !== 'string') ? obj.length !== undefined : false; 
}; 
win.isDefined = function(obj){ 
return typeof obj !== 'undefined'; 
}; 
win.error = function(msg) { 
throw new Error(msg); 
}; 
win.now = function() { 
return (new Date()).getTime(); 
}; 
win.print = function(value) { 
document.write(value); 
}; 
win.println = function(value) { 
print(value); 
document.write("<br/>"); 
}; 
win.each = function(object, callback, args) { 
var name, i = 0, 
length = object.length, 
isObj = (length === undefined || isFunction(object)); 
if (args) { 
if (isObj) { 
for (name in object) { 
if (callback.apply(object[name], args) === false) { 
break; 
} 
} 
} else { 
for (; i < length;) { 
if (callback.apply(object[i++], args) === false) { 
break; 
} 
} 
} 
} else { 
if (isObj) { 
for (name in object) { 
if (callback.call(object[name], name, object[name]) === false) { 
break; 
} 
} 
} else { 
for (; i < length;) { 
if (callback.call(object[i], i, object[i++]) === false) { 
break; 
} 
} 
} 
} 
return object; 
}; 
win.Array.prototype.toString = function(){ 
return "[" + this.join() + "]" 
} 
win.extend = function() { 
var options, 
name, 
src, 
copy, 
copyIsArray, 
clone, 
target = arguments[0] || {}, 
i = 1, 
length = arguments.length, 
deep = false; 
// Handle a deep copy situation 
if ( typeof target === "boolean" ) { 
deep = target; 
target = arguments[1] || {}; 
// skip the boolean and the target 
i = 2; 
} 
// Handle case when target is a string or something (possible in deep copy) 
if ( typeof target !== "object" && !isFunction(target) ) { 
target = {}; 
} 
// extend jQuery itself if only one argument is passed 
if ( length === i ) { 
target = this; 
--i; 
} 
for ( ; i < length; i++ ) { 
// Only deal with non-null/undefined values 
if ( (options = arguments[ i ]) != null ) { 
// Extend the base object 
for ( name in options ) { 
src = target[ name ]; 
copy = options[ name ]; 
// Prevent never-ending loop 
if ( target === copy ) { 
continue; 
} 
// Recurse if we're merging plain objects or arrays 
if ( deep && copy && ( isPlainObject(copy) || (copyIsArray = isArray(copy)) ) ) { 
if ( copyIsArray ) { 
copyIsArray = false; 
clone = src && isArray(src) ? src : []; 
} else { 
clone = src && isPlainObject(src) ? src : {}; 
} 
// Never move original objects, clone them 
target[ name ] = extend( deep, clone, copy ); 
// Don't bring in undefined values 
} else if ( copy !== undefined ) { 
target[ name ] = copy; 
} 
} 
} 
} 
// Return the modified object 
return target; 
}; 
})(window);

如果我们不用extend方法,可以象下面的方式写自己的组件:
(function(win){ 
win.StringBuffer = function(){ 
this.datas = []; 
} 
var proto = StringBuffer.prototype; 
proto.append = function(value){ 
this.datas.push(value); 
}, 
proto.toString = function(){ 
return this.datas.join(""); 
} 
})(window);

如果使用extend方法,可以象下面这样写组件:
(function(win){ 
win.extend(win,{ 
StringBuilder : function(){ 
this.datas = []; 
} 
}); 
win.extend(StringBuilder.prototype, { 
append : function(value){ 
this.datas.push(value); 
}, 
toString : function(){ 
return this.datas.join(""); 
} 
}); 
})(window);

两种方法的效果一样,但是extend方法还可以做更多事件,比如插件式开发,跟第二种方式很象。
当然,如果你本来就想写jQuery插件,那就直接用jQuery的extend就可以啦。
Javascript 相关文章推荐
javascript 获取select下拉列表值的代码
Sep 07 Javascript
JavaScript取得鼠标绝对位置程序代码介绍
Sep 16 Javascript
如何用JavaScript动态呼叫函数(两种方式)
May 03 Javascript
点击弹出层外区域关闭弹出层jquery特效示例
Aug 25 Javascript
AngularJS基础 ng-keydown 指令简单示例
Aug 02 Javascript
微信小程序 Page()函数详解
Oct 17 Javascript
js获取浏览器和屏幕的各种宽度高度
Feb 22 Javascript
layUI实现三级导航菜单效果
Jul 26 Javascript
vue+moment实现倒计时效果
Aug 26 Javascript
vue 使用外部JS与调用原生API操作示例
Dec 02 Javascript
JavaScript实现简单图片切换
Apr 29 Javascript
JS精髓原型链继承及构造函数继承问题纠正
Jun 16 Javascript
js不完美解决click和dblclick事件冲突问题
Jul 16 #Javascript
js jquery数组介绍
Jul 15 #Javascript
js限制文本框只能输入数字(正则表达式)
Jul 15 #Javascript
基于jquery的图片幻灯展示源码
Jul 15 #Javascript
20款非常优秀的 jQuery 工具提示插件 推荐
Jul 15 #Javascript
EasyUI 中 MenuButton 的使用方法
Jul 14 #Javascript
为EasyUI的Tab标签添加右键菜单的方法
Jul 14 #Javascript
You might like
深入理解PHP中的Session和Cookie
2013/06/21 PHP
PHP生成随机数的方法实例分析
2015/01/22 PHP
php使用COPY函数更新配置文件的方法
2015/06/18 PHP
php上传图片生成缩略图(GD库)
2016/01/06 PHP
降低PHP Redis内存占用
2017/03/23 PHP
iis6手工创建网站后无法运行php脚本的解决方法
2017/06/08 PHP
FCK调用方法..
2006/12/21 Javascript
模仿jQuery each函数的链式调用
2009/07/22 Javascript
js修改input的type属性及浏览器兼容问题探讨与解决
2013/01/23 Javascript
JQuery中如何传递参数如click(),change()等具体实现
2013/04/28 Javascript
jQuery实现时尚漂亮的弹出式对话框实例
2015/08/07 Javascript
Angular之指令Directive用法详解
2017/03/01 Javascript
JS实现的自动打字效果示例
2017/03/10 Javascript
值得分享和收藏的xmlplus组件学习教程
2017/05/05 Javascript
详解windows下vue-cli及webpack 构建网站(二)导入bootstrap样式
2017/06/17 Javascript
JS实现定时任务每隔N秒请求后台setInterval定时和ajax请求问题
2017/10/15 Javascript
在vue项目中优雅的使用SVG的方法实例详解
2018/12/03 Javascript
详解datagrid使用方法(重要)
2020/11/06 Javascript
微信小程序自定义底部弹出框动画
2020/11/18 Javascript
vue form表单post请求结合Servlet实现文件上传功能
2021/01/22 Vue.js
[01:38]完美世界DOTA2联赛PWL S3 集锦第四期
2020/12/21 DOTA
Python datetime时间格式化去掉前导0
2014/07/31 Python
python实现将文本转换成语音的方法
2015/05/28 Python
Django中URLconf和include()的协同工作方法
2015/07/20 Python
Ubuntu下安装PyV8
2016/03/13 Python
django框架如何集成celery进行开发
2017/05/24 Python
Python如何发布程序的详细教程
2018/10/09 Python
python判断完全平方数的方法
2018/11/13 Python
python实现tail -f 功能
2020/01/17 Python
python 发送get请求接口详解
2020/11/17 Python
详解python中的异常捕获
2020/12/15 Python
致跳远、跳高运动员广播稿
2014/01/09 职场文书
《学会待客》教学反思
2014/02/22 职场文书
开工仪式策划方案
2014/05/23 职场文书
计算机科学技术自荐信
2014/06/12 职场文书
java.util.NoSuchElementException原因及两种解决方法
2022/06/28 Java/Android