日期:2014-05-16  浏览次数:20348 次

js语法学习,注意点1

1.变量

? var test = 5;

? test = "hello";

? newtest = "no declare";

JS是弱类型语言,变量可以存放不同类型的值。

不一定需要声明,会在初始化,给变量名创建一个全局变量,再进行初始化

?

? ECMAScript原始类型:Undefined,Null,Number,Boolean,String.

? 原始值大小是不变的,所以放在栈中,引用值,数据大小是可变化的,会降低变量查询速度,所以不放在栈中,放在堆中

?

?

1.1 typeof

???? t = "fw";

???? typeof(t);

???? 对值或变量的调用将返回以下值

???? 1."undefined" 当变量是Undefined时

???? 2."boolean" 当变量是Boolean时

???? 3."string" 当变量是String时

???? 4."number" 当变量是数值时

???? 5."object" 当变量为Null或引用类型时

?1.2 Undefined

???? var t;

???? typeof(t);//the value is undefined

???? typeof(t1);//the value is undefined

???? alert(typeof(t)==typeof(t1))//the value is true

???? alert(t == undefined)//the value is true

???? alert(t1 == undefined)//exception

???? JS函数什么都不返回时,默认返回的是undefined

?1.3 Null

???? alert(null == undefined); //return is true

?1.4 Number

???? var x = 21;

???? x = 07;//八进制数

???? x = 0xab;//十六进制数

???? x = 0xAB;

???? alert(0xab == 0xAB);//true

??? 输入可以是八进制,也可以是十六进制的,但经过数值计算返回的都是十进制

??? 浮点数可以表示为

??? x = 9.32e8;

??? x = 9.323-e8;

??? 最大的浮点数为Number.MAX_VALUE,最小的浮点数为Number.MIN_VALUE

??? 所以有数都必须在他们之间,但计算的结果可以例外

??? +无穷大为 Number.POSITIVE_INFINITY

??? -无穷大为? Number.NEGATIVE_INFINITY

??? 注意? Number.POSITIVE_INFINITY == Number.POSITIVE_INFINITY 为true

?

??? 函数isFinite()可以判断函数是否为无穷大 isFinite(a*b)

?

??? NaN 发生在数值转换失败时,表示非数

??? NaN == NaN return false

??? isNaN()判断值是否是数值类型 isNaN("ewjifj")

?2 转换

??? 三种基本对象Number,String,Boolean 都是伪对象

?

??? 2.1 转换成字符串

??? 可以调用他们的toString()方法,转换为字符串

?

??? Boolean

??? var t = true;

??? t.toString()

?

??? Number 两种模式

??? 默认模式

??? var t = 89;

??? t.toString();

??? 基模式

??? t.toString(2);

??? t.toString(7);

?

?

???

??? 2.2转换成数值

??? 调用parseInt(),parseFloat()

??? 会检查,从左开始,遇到无效数字退出

?