核心javascript内置对象,即ecmascript实现提供的不依赖于宿主环境的对象
这些对象在程序执行之前就已经(实例化)存在了。ecmascript称为the global object,分为以下几种
1, 值属性的全局对象(value properties of the global object)。有nan,infinity,undefined。
2, 函数属性的全局对象(function properties of the global object)。有eval,parseint,parsefloat,isnan,isfinite,decodeuri,encodeduri,encodeuricomponent
3,构造器(类)属性的全局对象(constructor properties of the global object)。有object,function,array,string,boolean,number,date,regexp,error,evalerror,rangeerror,referenceerror,syntaxerror,typeerror,urierror。
4,其它属性的全局对象(other properties of the global object),可以看出成是java中的静态类,可以直接用类名+点号+方法名使用。有math,json。
ecmascript规范提到这些全局对象(the global object)是具有writable属性的,即writable为true,枚举性(enumerable)为false,即不能用for in枚举。ecmascript有这么一段
unless otherwise specified, the standard built-in properties of the global object have attributes {[[writable]]: true, [[enumerable]]: false, [[configurable]]: true}.
虽然规范提到the global object是可以被重写的,但不会有谁去重写它们的。这里仅仅做个测试。
nan = 11;
eval = 22;
object = 33;
math = 44;
alert(nan);
alert(eval);
alert(object);
alert(math);<br>
分别取值属性的全局对象, 函数属性的全局对象,构造器(类)属性的全局对象,其它属性的全局对象nan,eval,object,math。结果如下
结果可以看出除了nan在ie9(pre3)/safari不能被重写外,其它都被重写了。这里只是列举了四个,感兴趣的可以将以上所有的the global object一一测试下。这里想表达的是核心javascript内置对象一般是可以被重写的 ,虽然没人这么干。
下面测试下其可枚举性
for(var a in nan){
alert(a);
}
for(var a in eval){
alert(a);
}
for(var a in object){
alert(a);
}
for(var a in math){
alert(a);
}
所有浏览器都没有弹出,即属性不被枚举。感兴趣的可以将以上所有的the global object的枚举性一一测试下。当然对于有些浏览器如firefox,某些global object被重写后又是可以被枚举的。
以上就是核心javascript内置全局对象/函数实例详解的详细内容。