您的位置:首页 > Web前端 > JavaScript

JAVASCRIPT基础学习篇(5)--ECMAScript Basic1(EcmaScript 基本类型)

2009-02-23 11:05 471 查看
第一章 基本类型

一、The basic concepts of ECMAScript are the following:(最基本语法)
1、Everything is case-sensitiv(区分大小写)
2、Variables are loosely typed。(松散类型变量)
只需使用var关键字来定义变量如:var visible = true;
End-of-line semicolons are optional(行尾的分号是可选的即不一定有)
如以下均可:var test1 = “red” var test2 = “blue”;
3、Comments are the same as in Java, C, and Perl(注释与其它如java一样可使用//或/* */来注释。)
4、Braces indicate code blocks(括号表明代码块)

二、Variables
1、 变量声明:
var test = “hi”, age = 25;
声明两个变量test,age,javascript会自动创建一个String类型和一个int类型的变量。
变量不一定在声明的时候就初始化值,如:var test;即声明了一个变量test但未初始化。
变量可以赋以不同类型的值如:
var test = 'sfsf';
test = 40
以上是可以的,这都是因为JS的松散类型的优点所在。这些在JAVA中是不行的。

2、 变量名约束:
In terms of variables names, a name must follow two simple rules:
The first character must be a letter, an underscore (_), or a dollar sign ($).
All remaining characters may be underscores, dollar signs, or any alphanumeric characters.
All the following variable names are legal:
var test;
var $test;
var $1;
var _$te$t2;

3、变量名的写法
有三种写法如下:
Camel Notation:(第一个单词小写,以后每个单词首字母大写)
var myTestValue = 0, mySecondTestValue = “hi”;
Pascal Notation:(每个单词首字母大写)
var MyTestValue = 0, MySecondTestValue = “hi”;
Hungarian Type Notation:(每个单词首字母大写,但在最前面加上变量类型符)
var iMyTestValue = 0, sMySecondTestValue = “hi”;

4、变量在使用之前不声明的情况
如下:var test ="hello"
test2 = test + "world"
alert(test2)
结果为: hello world
注意的是这里javascript默认创建了test2变量,并且作为全局变量,所以使用的时候要注意,因为在程序中某一处改变了这个变量的值,则结果即将随之改变

三、关键字
以下关键字不能作为变量或方法名
break else new var
case finally return void
catch for switch while
continue function this with
default if throw
delete in try
do instanceof typeof

保留字
以下保留字不能作为变量或方法名,留作将来使用
abstract enum int short
boolean export interface static
byte extends long super
char final native synchronized
class float package throws
const goto private transient
debugger implements protected volatile
double import public

四、基本类型变量与引用类型变量(Primitive and Reference Values)
Primitive values are simple pieces of data that are stored on the stack, which is to say that their
value is stored directly in the location that the variable accesses.
基本类型变量存储在栈中,它的值直接存储在变量所指的空间
Reference values, on the other hand, are objects that are stored in the heap, meaning that the value stored in the variable location is a pointer to a location in memory where the object is stored.
引用类型存储在堆中,它通过一个对象指向它
ECMAScript primitive types(基本类型变量有):Undefined, Null, Boolean, Number, or String
In many languages, strings are considered a reference type and not a primitive type because a string can vary in length. ECMAScript breaks from this tradition.
在许多语言中,String类型因为其长度可以改变而被当作引用类型变量,但在ECMAScript中打破了传统

五、typeof操作符
用于判断变量的类型
The typeof operator takes one parameter: the variable or value to check.
typeof(50) //parameter is value
var str = "sfsfsf"
typeof(str) //parameter is variable
typeof可能返回的值
“undefined” if the variable is of the Undefined type.
(当一个变量只声明,但没有赋值时,那个这个变量默认的值为undefined,使用typeof将返回它的类型为undefined
“boolean” if the variable is of the Boolean type.
“number” if the variable is of the Number type.
“string” if the variable is of the String type.
“object” if the variable is of a reference type or of the Null type.

六、The Undefined type
Note that a variable having the value of undefined is different from a value being undefined
需要注意的是一个变量的值未定义(即变量没有被初始化)不能说成是变量未定义
如下:
var temp;
alert(typeof temp) //返回undefined,但变量temp已经声明
alert(typeof temp2); //也返回undefined,但变量temp2从未声明
注意的是如果这样写:
alert(temp) //返回undefined
alert(temp2) //报错提示:temp2未定义,未定义的变量不能参与运算。
所以,typeof返回undefined时表示变量没有初始化或变量不存在
当一个函数没有明确的返回值时,将返回undefined
function testFunc() {
//leave the function blank
}
alert(testFunc() == undefined); //outputs “true”
alert(typeof testFunc()) //outputs "undefined"

七、The Null type
the Null type, has only the special value null
alert(null == undefined); //outputs “true”
Even though the values are both true, they are considered to have different meanings. Whereas
undefined is the value assigned when a variable is declared and not initialized, null is the value used
to represent an object that doesn’t exist
返回为true,但null和undefined的意义不同

八、The Boolean type
It has two values, true and false。
Even though false isn’t equal to 0, 0 is converted to false
布尔值为true或false,但js中把非0当作true,0当作false

九、The Number type
定义整型:var iNum = 55;
定义8进制整型: var iNum = 070;
For an octal literal,the first digit must be a zero (0), and the following digits can be any octal digit (0 through 7)
定义十六进制:var iNum = 0x1f
To create a hexadecimal literal, the first digit must be a zero (0) followed by the letter x, followed by any
number of hexadecimal digits (0-9 and A-F).
定义浮点型
var s = 4.5;
The interesting thing about this form of floating-point literal is that it is actually stored as a string until it’s needed for calculation.
实际上浮点型存储的是字符串型,直到运算的时候
var fNum = 3.125e7; 表赤的中3.125 * 107

INFINITY
finite有限的,infinite无限的、无穷的
When a calculation results in a number greater than Number.MAX_VALUE, it is assigned a value of
Number.POSITIVE_INFINITY, meaning that it has no numeric value anymore. Likewise a calculation
that results in a number less than Number.MIN_VALUE is assigned a value of Number.NEGATIVE_
INFINITY, which also has no numeric value. If a calculation returns an infinite value, the result cannot
be used in any further calculations.
Infinity. Number.POSITIVE_INFINITY has a value of Infinity, whereas Number.NEGATIVE_INFINITY has a value of –Infinity.
当一个数字大于1.7976931348623157E+10308时正无穷,当一个数字小于-1.7976931348623157E+10308时负无穷。
任何数除以无穷大为0,任何数乘以无穷大为无穷大。
The isFinite() method can be called on any number to ensure that the number isn’t infinite.
var iResult = iNum* some_really_large_number;
if (isFinite(iResult)) {
alert(“Number is finite.”);
} else {
alert(“Number is infinite.”);
}

NaN :Not a Number非数值
alert(NaN == NaN); //outputs “false”
alert(isNaN(“blue”)); //outputs “true”
alert(isNaN(“123”)); //outputs “false”

十、The String type
The String type is unique in that it is the only primitive type that doesn’t have a definite size.
是基本类型中唯一一种具有非固定长度的类型
String literals are specified by using either double quotes (“) or single quotes (‘).
字符串类型可用双引号或单引号包括起来,这与在java等语言中是不同的。
Literal Meaning
/n Newline
/t Tab
/b Backspace
/r Carriage return
/f Formfeed
// Backslash
/’ Single quote
/” Double quote
/0nnn A character represented by octal code nnn (where n is an octal digit 0-7)
/xnn A character represented by hexadecimal code nn (where n is a hexadecimal digit 0-F)
/unnnn A Unicode character represented by hexadecimal code nnnn (where n is a hexadecimal digit 0-F)

十一、Converting to a string
toString()方法:
布尔类型toString()
var bFound = false;
alert(bFound.toString()); //outputs “false”
数值类型的toString(),可带进制参数值
var iNum1 = 10;
var fNum2 = 10.0;
alert(iNum1.toString()); //outputs “10”
alert(fNum2.toString()); //outputs “10”

var iNum = 10;
alert(iNum1.toString(2)); //outputs “1010”
alert(iNum1.toString(8)); //outputs “12”
alert(iNum1.toString(16)); //outputs “A”

十二、Converting to a number
parseInt()
var iNum1 = parseInt(“1234blue”); //returns 1234
var iNum2 = parseInt(“0xA”); //returns 10
var iNum3 = parseInt(“22.5”); //returns 22
var iNum4 = parseInt(“blue”); //returns NaN
带进制参数的转換
var iNum1 = parseInt(“10”, 2); //returns 2
var iNum2 = parseInt(“10”, 8); //returns 8
var iNum2 = parseInt(“10”, 10); //returns 10
以下如果数值有前导符0,则默认为8进制转換
var iNum1 = parseInt(“010”); //returns 8
var iNum2 = parseInt(“010”, 8); //returns 8
var iNum3 = parseInt(“010”, 10); //returns 10

parseFloat()
The parseFloat() method works in a similar way to parseInt(), looking at each character starting in
position 0. It also continues until the first invalid character and then converts the string it has seen up to that point. For this method, however, the decimal point is a valid character the first time it appears. If two decimal points are present, the second is considered invalid and the parseFloat() method converts the string up until that position. This means that the string “22.34.5” will be parsed into 22.34.
在原字符串的第二个小数点处不再转换
Another difference when using parseFloat() is that the string must represent a floating-point number
in decimal form, not octal or hexadecimal. This method ignores leading zeros, so the octal number 0908 will be parsed into 908, and the hexadecimal number 0xA will return NaN because x isn’t a valid character for a floating-point number. There is also no radix mode for parseFloat()与.
与parseInt的第二点不同是:没有第二个代表进制数的参数,并且忽略前导字符0
如:
var fNum1 = parseFloat(“1234blue”); //returns 1234.0
var fNum2 = parseFloat(“0xA”); //returns NaN
var fNum3 = parseFloat(“22.5”); //returns 22.5
var fNum4 = parseFloat(“22.34.5”); //returns 22.34
var fNum5 = parseFloat(“0908”); //returns 908
var fNum6 = parseFloat(“blue”); //returns NaN

十三、Type Casting
Three type casts are available in ECMAScript
Boolean(value) – casts the given value as a Boolean
Number(value) – casts the given value as a number (either integer or floating-point)
String(value) – casts the given value a string

The Boolean() type cast returns true when the value is a string with at least one character, a number
other than 0, or an object (discussed in the next section); it returns false when the value is an empty
string, the number 0, undefined, or null.
示例如下:
var b1 = Boolean(“”); //false – empty string
var b2 = Boolean(“hi”); //true – non-empty string
var b3 = Boolean(100); //true – non-zero number
var b4 = Boolean(null); //false - null
var b5 = Boolean(0); //false - zero
var b6 = Boolean(new Object()); //true – object

The Number() type cast works in a manner similar to parseInt() and parseFloat(), except that it
converts the entire value, not just part of it.
Remember that parseInt() and parseFloat() only convert up to the first invalid character (in strings), so “4.5.6” becomes “4.5”. Using the Number() type cast, “4.5.6” becomes NaN
示例如下:
Number(false) 0
Number(true) 1
Number(undefined) NaN
Number(null) 0
Number(“5.5”) 5.5
Number(“56”) 56
Number(“5.6.7”) NaN
Number(new Object()) NaN
Number(100) 100
转換成字符串,使用String()与使用toString()的区别,不能使用null.toString()
The only difference between type casting as a string and using toString() is that the type cast can produce a string for a null or undefined value without error:
var s1 = String(null); //”null”
var oNull = null;
var s2 = oNull.toString(); //won’t work, causes an error
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐