implements |
package |
public |
interface |
private |
static |
let |
protected |
yield |
|
|
|
##Note that let and yield are new reserved words in the 5th edition; other reserved words are defined in the 3rd edition. In order to ensure compatibility to the greatest extent, it is recommended that you use the reserved words defined in the 3rd edition plus let and yield as a reference when programming.
Using keywords as identifiers in JavaScript engines that implement ECMAScript 3 will cause "Identifier Expected" errors. Using reserved words as identifiers may or may not result in the same error, depending on the specific engine.
The 5th edition has slightly modified the rules for using keywords and reserved words. Keywords and reserved words, although still not available as identifiers, can now be used as object property names. In general, it is best not to use keywords and reserved words as identifiers and property names to maintain compatibility with future ECMAScript versions.
In addition to the reserved words and keywords listed above, ECMA-262 5th Edition imposes restrictions on eval and arguments. In strict mode, these two names cannot be used as identifiers or attribute names, otherwise an error will be thrown.
2 Data types
There are five simple data types (also called basic data types) in ECMAscript: Undefined .Boolean, String, Null, Number. There is also a complex data type (sometimes called a reference data type) - Object. Object is essentially a set of unordered key-value pairs. ECMAscript does not support any mechanism for creating custom types, and all values are one of the six data types mentioned above.
typeof
This operator (not a function here, because it is not a function) is generally used during debugging. There are six return values: undefined (value is definition), boolean (Boolean value), string (string), number (number), object (this value is an object or null), function (this value is a function).
Corresponding to the regular expression, it is actually another form of object (RegExp type object), but for versions before Chrome7 and Safari5, function is returned and other browsers return object
Let's take a look at a simple example
var msg; //如果没有赋值,默认是undefinedtypeof msg; //undefined// age 没有定义typeof age; //undefined
This result is logically reasonable. Because although these two variables are essentially different from a technical point of view, in fact, it is impossible for those variables to perform real operations.
Even if uninitialized variables will automatically be assigned undefined, it is still a wise choice to initialize variables explicitly. If this can be done, then when the typeof operator returns undefined, we can be sure This variable has not been declared yet
The relationship between null and undefined
console.log(undefined == null) // trueconsole.log(undefined === null) // false
In fact, the undefined value is derived from null. This is because ECMA-262 stipulates that their equality must return true.
Although null and undefined have such a relationship, their uses are completely different. There is no need to set the value of a variable to undefined under any circumstances, but the same lady rule is inappropriate for null. I think that as long as the object variable is intended to be saved but the real variable has not been saved, the variable should be explicitly saved as null. This can not only reflect the convention of null as a null pointer, but also further distinguish null and undefined
Number type
Number type should be The most concerning data type in ECMAscript, this type uses IEEE754 to represent integers and floating point values
var num1 = 070; //表示八进制数var num2 = 079 //无效的八进制数 表示79var num3 = 0x1f //十六进制数
Octal literals are invalid in strict mode. Will cause the javascript engine to report an error
Floating point value
<span style='font-family: 微软雅黑, "Microsoft YaHei";'>var f1 = 0.1; <br>var f2 = .1; //虽然是有效的0.1但是不推荐var f3 = 1.0; //会自动转换为1</span>
For some very large or very small values, scientific notation can also be used. ,
var f4 = 3.125e7; //31250000
在这里还是要提示一下众所周知的浮点数不能比较相等法则,
在javascript中浮点数的最高精度是17位的小数,但在进行算术计算的时候精确度远远不如整数,例如0.1+0.2不等于0.3而是等于0.0000000000000004(4e-17)
这是使用基于IEEE754数据浮点计算的通病
数据范围
由于内存的限制,ECMAscript并不能保存世界上所有的数,能保存的数能在浏览器中显示出来(通过Number.MAX_VALUE),最小值是(Number.MIN_VALUE)
当超出这个范围的时候,大于就变为Infinity,小于就变为-Infinity
<span style='font-family: 微软雅黑, "Microsoft YaHei";'>var big = Number.MAX_VALUE + Number.MAX_VALUE;<br>console.log(big); //Infinity</span>
可以使用isFinite()函数判断这个值是否在最大值和最小值之间,如果在就返回true
通过访问Number.NEGATIVE_INFINITY和Number.POSITIVE_INFINITY来得到-Infinity和Infinity
NaN
NaN,非数值类型(Not a Number)是一个特殊的值,这个数值表示一个应该返回数值的情况结果没有返回数值,在ECMAscript中,任何数值除以0并不会导致错误,会返回一个NaN,因此并不会影响代码的运行。
Nan本身有两个特点:任何设置NaN的操作都会返回NaN,这个特点在多步计算的时候可能导致错误,其次Nan和任何数值都不想等,包括本身:
<span style='font-family: 微软雅黑, "Microsoft YaHei";'>console.log(NaN == Infinity)<br>console.log(NaN == 1)<br>console.log(NaN == NaN)<br>console.log(NaN == 1/0)//以上结果返回的都是false//下面是判断数据类型isNaN(NaN) //trueisNaN('123') //false,进行了数据类型的转换isNaN('f5') //trueisNaN(4) //false</span>
运用isNaN()函数可以判断这个变量是否是NaN类型的。
最神奇的是isNaN也是支持对象操作的,在基于对象调用isNaN函数时,会首先调用对象的valueOf方法,然后确定该方法返回的值是否可以转换为数值,如果不能就基于这个返回值再调用toString()方法,在测试返回值
string
对应于string类型,讨论的会比较少一点,string类型用于友零或者多个16位的Unicode字符组成的字符序列,即字符串。js中的单引号和双引号是没有区别的(想对于PHP来讲)。
字符字面量
转义的只是在这里就不多说了,
字面量在这里只说一点:
var text = "this is \u30a3"这个的长度是9因为\u30a3转义后表示一个字符
字符串的特点
字符串在我们平常的开发当中我们是一直在用的,但是很少人知道其中的原理,下面我们就来讨论一下字符串的特点。
ECMAscript中的字符串是不可变的,也就是说,字符串一旦被创建,他们的值就不能被改变。要改变某个变量保存的字符串就先要销毁原来的字符串,然后用一个新的值来填充该变量,例如下面的代码:
var lang = 'java';
lang = lang + 'script';
以上的代码是变量lang中包含字符串‘java’。而第二行代码把lang的值重新定义为‘java’与’script’的结合,实际上这个操作过程如下:
1. 首先创建一个能容纳10个字符的新字符串
2. 然后将这个字符串中填充’java’和’script’
3. 最后一步是销毁原来的’java’字符串和’script’字符串,因为这两个字符串已经没用了
转换为字符串
在这里主要注意的是null和undefined是没有toString()方法的,如果想要把他们转换为字符串可以使用转型函数String(),能把任何类型的值转换为字符串var val = null;String(val)
当使用数值转换为字符串的时候,可以在toString()传递一个参数,表示把数值变为多少进制的字符串:var num = 10;num.toString(2)返回的是1010;
4 Object类型
ECMAscript中对象其实就是一组数据和功能的集合。
三种实例化一个对象的方法:
var o = new Ocject();var o = new Object;//不推荐使用var o = {};//当今最常用的
object中的对象和属性在后面我们在去讨论
5 JavaScript中+的作用
这个我们在这里说的具体一点
1. 加法运算
console.log(1+2)
字符串的连接
<span style='font-family: 微软雅黑, "Microsoft YaHei";'>var a ="121",<br> b =56,<br> c ="ss";<br>console.log(a+b);//12156console.log(a+c);//121ss</span>
数值和字符串之间的转换
<span style="font-family: 微软雅黑, "Microsoft YaHei";">//字符串转换我数值var a = "21212";<br/>+a //21212var b = "ss";<br/>+a //NaNvar c= "0xff";<br/>+a //255</span>
<span style='font-family: 微软雅黑, "Microsoft YaHei";'>//数值转换为字符串var a =45;<br>+a; //"45"</span>
//布尔值的转换+true //1+fasle //0
6 javascript中-的作用
减号的作用一般是都是用在正规的操作(也就是类型相同)的操作中,信息量虽然不是很大,但是还是有很多需要注意的地方,下面来举几个例子
var a = 5-true; //4 ,true被转换了var b = NaN -1;//NaNvar c= 5-2 //3var d = 5-"2";//3 “2”被转换为2var e = 5-null; //5 null被转换为了0
7 相等操作符
在我们编程的时候,当使用比较的时候我们希望,有的时候类型不同可以数值一样就可以就好像(2和"2")是可以相等的,但是有时候我们希望要求数值和类型都是一样的才返回真。这就很矛盾了,在ECMAscript中的解决方案是提供两组操作数:相等和全等
相等:先转换在比较
列出一下比较特殊的比较结果
表达式 |
值 |
null == undefined |
true |
“NaN” == NaN |
false |
NaN == NaN |
false |
NaN != NaN |
true |
true == 2 |
false |
undefined == 0 |
false |
null == 0 |
false |
全等:仅比较不转换
全等就比较严格了,在这里就不讨论什么了。
注意:null == undefined会返回true,但是null === undefined会返回false,这是由于他们的类型不一样
8 函数
函数对于任何语言来说都是一个核心的概念。
在这里主要说一个问题,细节会在后面继续讨论
我们都是知道在函数中的arguments表示所有传进来的参数,比如说
<span style='font-family: 微软雅黑, "Microsoft YaHei";'>function foo(a,b,c){<br> console.log(arguments[2] === c);//true}</span>
这个原因并不因为他们指向的是同一块内存,因为他们内存空间的值会相互同步,
arguments的值是不能被改写的如果改写的会操作
相关推荐:
JavaScript初学者不知道的事(一)
javascript初学者实践使用
给JavaScript初学者的一些最佳实践