关于Math.PI、前自增和后自增

作者:Fdream 来源:Fdream博客 时间:2009-05-25 12:38:00 

有这么一个题目,说bt其实也不bt,为了重点突出其中的意图,特意加上了括号:

var a = (++Math.PI); 
var b = (Math.PI++); 
var c = Math.PI = (++Math.PI); 
var d = Math.PI = (Math.PI++); 
var e = Math.PI = (Math.PI + 1); 

执行完后,a、b、c、d、e的值分别是什么呢?如果学校里学的C语言基础好的话,应该可以知道答案。你不妨先写下答案,我们再来看看ECMA的规范。

在ECMA-262中,对于Math.PI的说明是这样的:

15.8.1.6


The number value for π, the ratio of the circumference of a circle to its diameter, which is approximately 3.1415926535897932.

This property has the attributes { DontEnum, DontDelete, ReadOnly }.

很清楚,ReadOnly,那么你给Math.PI赋值是没有用的。

再看看关于前自增(Prefix Increment):

11.4.4 Prefix Increment Operator


The production UnaryExpression : ++ UnaryExpression is evaluated as follows:

1. Evaluate UnaryExpression.

2. Call GetValue(Result(1)).

3. Call ToNumber(Result(2)).

4. Add the value 1 to Result(3), using the same rules as for the + operator (see 11.6.3).

5. Call PutValue(Result(1), Result(4)).

6. Return Result(4).

注意,前自增返回的是第四步的结果,也就是自增1以后的值。因此,对于a来说,就是先取Math.PI的值,然后转化为Number,然后加1,返回的就是Math.PI的值加1以后的结果。因此,a是4.141592653589793.

再看看关于后缀自增(Prefix Increment):

11.3.1 Postfix Increment Operator


The production PostfixExpression : LeftHandSideExpression [no LineTerminator here] ++ is evaluated as follows:

1. Evaluate LeftHandSideExpression.

2. Call GetValue(Result(1)).

3. Call ToNumber(Result(2)).

4. Add the value 1 to Result(3), using the same rules as for the + operator (see 11.6.3).

5. Call PutValue(Result(1), Result(4)).

6. Return Result(3).

和上面的不同,返回第三步的结果,即没有自增之前的结果,因此返回3.141592653589793.

再来看看两个连续赋值的三个表达式,由于赋值运算是右结合运算,因此这三个表达式相当于:

var c = (Math.PI = (++Math.PI)); 
var d = (Math.PI = (Math.PI++)); 
var e = (Math.PI = (Math.PI + 1)); 

而对于赋值运算,在ECMA-262中是这样描述的:

11.13.1 Simple Assignment ( = )


The production AssignmentExpression : LeftHandSideExpression = AssignmentExpression is evaluated as follows:

1. Evaluate LeftHandSideExpression.

2. Evaluate AssignmentExpression.

3. Call GetValue(Result(2)).

4. Call PutValue(Result(1), Result(3)).

5. Return Result(3).

赋值后,返回第三步的结果,即语句右边的表达式的结果。因此,这里c、d、e的结果就很明显了:

c = 4.141592653589793; 
d = 3.141592653589793; 
e = 4.141592653589793; 

这里还要注意一点什么?a++ 和 a=a+1 不一样~

标签:javascript,math,pi,自增
0
投稿

猜你喜欢

  • 高效地获取XMLhttp对象

    2010-01-19 13:49:00
  • 泛泛而谈界面中的斑马纹设计

    2010-07-15 12:59:00
  • SQL SERVER EXPRESS 常见问题及解决办法

    2008-09-13 19:07:00
  • 十六则Dreamweaver使用快技法

    2009-07-05 18:55:00
  • SQL Server 2000 清理日志精品图文教程

    2012-07-21 14:31:17
  • 解析SQL server与asp 互操作的时间处理

    2009-02-05 16:13:00
  • 使用CSS3和RGBa创建超酷的按钮

    2009-06-02 12:41:00
  • 用ASP木马实现FTP和解压缩

    2008-02-13 08:47:00
  • 用ASP实现txt,doc,jpg等文件下载的函数

    2007-08-17 13:17:00
  • 如何在Mac OS X中安装MySQL

    2009-09-01 10:16:00
  • AJAX实现web页面中级联菜单的设计

    2007-09-26 13:37:00
  • 概念性产品设计

    2008-06-11 12:57:00
  • 详细讲解删除SQL Server日志的具体方法

    2008-12-09 14:32:00
  • 在asp中调用sql server的存储过程方法

    2007-08-13 13:28:00
  • 《设计网事》前言

    2009-07-15 17:19:00
  • SQL存储过程介绍

    2008-02-13 18:52:00
  • 客户端模板的应用

    2007-05-11 16:50:00
  • Dreamweaver4探谜系列(1)

    2010-09-05 21:12:00
  • ASP中RegExp对象正则表达式语法及相关例子

    2007-08-12 17:46:00
  • SQL SERVER 2005中的同步复制技术

    2009-01-05 13:44:00
  • asp之家 网络编程 m.aspxhome.com