js的运算
1.表达式
表达式是EMCAscript中的一个“短语”,解释器会通过计算把它转换成一个值。最简单的表达式是字面量或者变量名。
2.前置递增(++box)和后置递增(box++)的区别
看下面一段代码的实现过程:
var box = 100; alert(box++); // 100 box = box+1;先执行打印box,再赋值,100 + 1 = 101 alert(++box); // 102 box = 1+box;先赋值,再打印box, 101 + 1 = 102 age = box++; height = ++box; alert(age); //102 alert(height); //104
对比一下是不是这样:
var box = 100; age = box++; alert(age); //100
var box = 100; age = ++box; alert(age); //101
3.++号的隐含的Number的转换
3.1字符串
var box = '89'; var box1 = 'lc'; alert(typeof box); //string alert(typeof box1); //string age = ++box; age1 = ++box1; alert(typeof box); //number alert(typeof box1); //number alert(typeof age); //number alert(age1); //NaN alert(typeof age1); //number
3.2 Boolean
var box1 = false; box1++; //将false转换成0,0+1=1 alert(box1); // var box2 = true; box2++; //将true转换成1, 1+1=2 alert(box2); //2
3.3对象Object
var box= {};
alert(box); //[object object]
box++;
alert(box); //NaN
box = {
toString:function(){
return '123'
}
}
alert(typeof box); //object
box++;
alert(box); //124
alert(typeof box); //number
var box1 = {
toString :function(){
return 'lc'
}
};
box1++;
alert(box1); //NaN
alert(typeof box1); //number
4+号也有这样的功能;但是注意一点
var box = '100'; +box; //这样并不能改变变量,变量也是不能改变,只能消掉重新创建变量。(这是底层原理) alert(box); //100 alert(typeof box);//stringalert(typeof +box);//number
4.1+号对string的转换
var box = false; alert(typeof box);//boolean alert(+box); //0 alert(typeof +box); //number var box1 = true; alert(+box1);//1
4.2Object
var box = {};//+box表正数,-box表示负数
alert(box);//[object object]
alert(typeof box); //object
alert(+box); //NaN
alert(typeof +box);//number
var box1 = {
toString:function(){
return '123'
}
};
alert(+box1);//123
alert(typeof +box1); //number
5运算
var box = 1 +NaN; alert(box);//NaN var box1 = 100 + '100'; alert(box1);//100100 alert(typeof box1);//string 运算规则是:number和string的运算有一个string,‘+’号就会变string的连字符
5.()括号强制优先级
var age = '年龄:'+10+20; alert(age);//年龄:1020 var age = '年龄:'+(10+20); //括号强制优先级 alert(age);//年龄:30
6.number 和object
var box = 10 + {};
alert(box); //10[object object]
alert(typeof box);//string 很奇怪
var box = 10 + {
toString:function(){
return '20';
}
};
alert(box);//1020
alert(typeof box);//string
var box = 10 = {
toString:function(){
return 20;
}
};
alert(box);//30
alert(typeof box);//number
其他的运算转换,以后补充
js的运算的更多相关文章
- js赋值运算的理解
简介 js引擎由于为了效率,很多时候的非直接量赋值都不是copy一份在赋值给新的变量,而是一个引用 ps:直接量:直接值数字字符串等 为什么使用len = doms.length; 里的len效率要比 ...
- JS浮点数运算Bug
JS浮点数运算Bug的解决办法(转) 37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.0849999 ...
- js浮点数运算需要注意的问题
最近在js运算浮点数时发现了一个问题.问题是这样的:js函数中处理两个浮点数的相加,为了防止出现0.1+0.2=0.30000000000000004的问题,两个数都先乘以10000后再相加,得到结果 ...
- 通过一张简单的图,让你彻底地搞懂JS的==运算
大家知道,JavaScript中的==是一种比较复杂运算,它的运算规则很奇怪,很容易让人犯错,从而成为JavaScript中“最糟糕的特性”之一. 在仔细阅读ECMAScript规范的基础上,我画了一 ...
- 一道JS 连续赋值运算的问题
原文链接:https://www.cnblogs.com/joesbell/p/6229423.html <script> var a = {n:1}; var b = a; a.x = ...
- js浮点数运算封装, 起因财务部分精确计算
目录 背景 具体代码 背景 项目中用到浮点数,Int 等 js中 Number类型比较多, 加上牵涉到财务软件, 前台js运算等. 有时候会出现精确度的问题 , 公共方法中有好事者写的方法. 此处拿来 ...
- 【转】JS浮点数运算Bug的解决办法
37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.08499999999998 怎么会这样,两个只有一 ...
- JS小数运算失精度的问题
JS因为是解释性语言,在运算中会有丢失精度的问题,这种现象多出现在浮点型运算的情况下. 例如 5.11 * 100 得到的结果是 511.00000000000006 这种情况尤其是在处理金额的时候 ...
- JS位运算和遍历
JS位运算符 整数 有符号整数:允许使用正数和负数,第32位作为符号位,前31位才是存储位 无符号整数:只允许用正数 如果用n代表位 位数 = 2^n-1 由于位数(1.2.4.8.16...)中只有 ...
- js的运算小数点的问题
问题这样的: 37.5*5.5=206.08 (JS算出来是这样的一个结果,我四舍五入取两位小数) 我先怀疑是四舍五入的问题,就直接用JS算了一个结果为:206.08499999999998 怎么会这 ...
随机推荐
- 你能不用计算机来计算S=a+(a+1)+(a+2) + ...... + b的解的数目吗?
S=a + (a + 1) + (a + 2) + ...... + b(其中a, b > 0) 现在我们要求,给定一个正整数S,求有多少种不同的<a,b>,使得上述的等式成立. 这 ...
- PHP 错误与异常 笔记与总结(7)将错误日志以邮件方式发送
当系统发生了很严重的问题,需要立刻发送给管理员.可以通过 error_log() 将错误以邮件形式发送到邮箱. 在 php.ini 中设置: sendmail_from = 472323087@qq. ...
- typecho流程原理和插件机制浅析(第二弹)
typecho流程原理和插件机制浅析(第二弹) 兜兜 393 2014年04月02日 发布 推荐 1 推荐 收藏 14 收藏,3.7k 浏览 上一次说了 Typecho 大致的流程,今天简单说一下插件 ...
- MySQL优化常用
一.mysql的配置都是小写的,使用下划线_或破折号-分割单词,两者是一样的二.在配置文件中可以用1m,1g等单位,但是用set命令,不能使用单位,默认单位是字节三.特殊例子a.query_cache ...
- Artificial Intelligence Research Methodologies 人工智能研究方法
Computer Science An Overview _J. Glenn Brookshear _11th Edition To appreciate the field of artificia ...
- a computer-centered view of information systems to a database-centered view
Code.Complete.Second.Edition 2004 Bachman compared the Ptolemaic-to-Copernican change in astronomy t ...
- visual studio 2005 编fortran程序,运行后dos窗口显示问题
比如程序: program main implicit none write(*,*) "AAAAAAAAAAAAAAAAAAAAAAAA" stop end 虽然可以看见DOS窗 ...
- [转]如何编写和应用Java的自定义异常类
编写自定义异常类实际上是继承一个API标准异常类,用新定义的异常处理信息覆盖原有信息的过程.常用的编写自定义异常类的模式如下: public class CustomException exten ...
- spring-3-mvc-hello-world-example
http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/
- java endorsed
endorsed目录,充许你将一些特殊的类库放到其中以供项目使用. 官方说明: Specifying the -Djava.endorsed.dirs=lib/endorsed system p ...