当前位置: 首页 > 图文教程 > 网络编程 > Javascript > JavaScript 加号(+)运算符号

Javascript
纯JS半透明Tip效果代码
JavaScript 读URL参数增强改进版版
JS对URL字符串进行编码/解码分析
javescript完整操作Table的增加行,删除行的列子大全
js可填可选的下拉框
prototype Element学习笔记(篇一)
prototype Element学习笔记(篇二)
prototype Element学习笔记(Element篇三)
Prototype使用指南之selector.js说明
不唐突的JavaScript的七条准则整理收集
js判断变量是否空值的代码
Firefox getBoxObjectFor getBoundingClientRect联系
Div自动滚动到末尾的代码
javascript笔试题目附答案
javascript引导程序
js身份证验证超强脚本
JS给元素注册事件的代码
JavaScript函数、方法、对象代码
JS写的数字拼图小游戏代码[学习参考]
关于B/S判断浏览器断开的问题讨论

Javascript 中的 JavaScript 加号(+)运算符号


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-10   浏览: 397 ::
收藏到网摘: n/a

在一些框架中看到了类似这样的写法:+new Date();感觉有些怪,查阅了相关资料和一些网友的帮助.对此用法解释如下,希望对大家有所帮助,不合适的地方请大家指正! 一,对于引用类型对象(我指的是String,Date,Object,Array,Function,Boolean)的+运算符运算过程如下!
1,首先调用此对象的valueOf方法,得到返回数值A
2,然后把此数值A转换成数字,得到的是最终数值
我的测试如下:
复制代码 代码如下:

function w(s){
document.writeln("<br/>");
document.writeln(s);
document.writeln("<br/>-----------------------------");
}
String.prototype.valueOf=function(){return 1;};
w(+new String("sss"));//输出1
String.prototype.valueOf=function(){return "a";};
w(+new String("sss"));//输出NaN

Date.prototype.valueOf=function(){return 1;};
w(+new Date());//输出1
Date.prototype.valueOf=function(){return "a";};
w(+new Date());//输出NaN
Object.prototype.valueOf=function(){return 1;};
w(+{});//输出1
Object.prototype.valueOf=function(){return "a";};
w(+{});//输出NaN
Array.prototype.valueOf=function(){return 1;};
w(+[]);//输出1
Array.prototype.valueOf=function(){return "a";};
w(+[]);//输出NaN
var s=function(){};
Function.prototype.valueOf=function(){return 1;};
w(+s);//输出1
Function.prototype.valueOf=function(){return "a";};
w(+s);//输出NaN
Boolean.prototype.valueOf=function(){return 1;};
w(+new Boolean());//输出1
Boolean.prototype.valueOf=function(){return "a";};
w(+new Boolean());//输出NaN

二,对于基本数据数据类型,其值转换成数字
复制代码 代码如下:

w(+5);//输出5
w(+true);//输出1
w(+false);//输出0
w(+"ss");//输出NaN
w(+"111");//输出111