单体内置对象_单体内置对象

定义:由ECMAScript实现提供的,不依赖与宿主环境的对象,这些对象在ECMASript程序执行之前就已经存在了。

意思就是说,开发人员不必显示的实例化内置对象,因为他们已经实例化了。例如Object、Array和String。ECMA-262还定义了两个单体内置对象:Global和Math。

Global对象

在某种意义上他是一个兜底的的对象,所有在全局作用域中定义的属性和方法都是Global的属性。诸如isNaN(),isFinite(),parseInt(),parseFloat()。

1、URI编码方法

encodeURI()主要用于整个URI

encodeURIComponent()主要对URI的某一段

主要区别:encodeURI()不会对属于URI的特殊字符进行编码,例如冒号、正斜杠。encodeURIComponent()则会对所有非标准字符进行编码

1 var uri = "http://www.wrox.com/illegal value.html#start";2 console.log(encodeURI(uri)); //http://www.wrox.com/illegal%20value.html#start

3 console.log(encodeURIComponent(uri));4 //http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.html%23start

与encodeURI()和encodeURIComponent()对应的两个方法是decodeURI()和decodeURIComponent()

1 var uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.html%23start";2 console.log(decodeURI(uri)); //http%3A%2F%2Fwww.wrox.com%2Fillegal value.html%23start

3 console.log(decodeURIComponent(uri)); //http://www.wrox.com/illegal value.html#start

2、eval()方法

eval()方法就像是一个完整的ECMAScript解析器,它只接受一个参数,即要执行的ECMAScript字符串

1eval("alert('hi')");

相当于执行下面代码

1 alert("hi");

它会将传入的参数当做实际的ECMAScript语句来解析,然后把执行结果插入到原来位置。通过eval()执行的代码被认为是包含该次调用的执行环境的一部分,所以通过eval()

执行的代码可以引用在包含环境中定义的变量。

1 var msg = "hello word";2 eval(alert(msg));3

4 eval("function sayHi(){alert('hi')}");5 sayHi();

在eval()中创建的任何变量和函数都不会被提升,因为在解析代码的时候,它们被包含在一个字符串中,它们只在eval()执行的时候创建。并且在严格模式下,在外部访问不到eval()中创建的任何变量和函数

1 console.log(sayHi()); //sayHi is not defined

2 eval("function sayHi(){alert('hi')}");

3、Global对象的属性

属  性

说  明

属  性

说  明

undefined

特殊值undefined

Date

构造函数Date

NaN

特殊值NaN

RegExp

构造函数RegExp

Infinity

特殊值Infinity

Error

构造函数Error

Object

构造函数Object

EvalError

构造函数EvalError

Array

构造函数Array

RangeError

构造函数RangeError

Function

构造函数Function

ReferenceError

构造函数ReferenceError

Boolean

构造函数Boolean

SyntaxError

构造函数SyntaxError

String

构造函数String

TypeError

构造函数TypeError

Number

构造函数Number

URIError

构造函数URIError

4、Window对象

在Web浏览器中都是将这个全局对象作为window对象的一部分加以实现的,因此,在全局作用域中定义的所有变量和函数都成为了window对象的属性。

1 var color = "red";2 functionsayColor(){3 console.log(window.color);4 }5 window.sayColor(); //"red"

另一种取的Global对象的方法是使用以下代码:

1 var global = function(){2 return this;3 }();

Math对象

ECMAScript还为保存数学公式和信息提供了一个公共位置,即Math对象,与我们在javascript中编写的计算功能相比,Math对象提供的计算功能执行起来要快的多

1、Math对象的属性

属  性

说  明

Math.E

自然对数的底数,即常量的e的值

Math.LN10

10的自然对数

Math.LN2

2的自然对数

Math.LOG2E

以2为底e的对数

Math.LOG10E

以10为底e的对数

Math.PI

π的值

Math.SQRT1_2

1/2的平方根(即2的平方根的倒数)

Math.SQRT2

2的平方根

2、min()和max()方法

1 var max = Math.max(3,56,98,32);2 console.log(max); //98

3

4 var min = Math.min(3,56,98,32);5 console.log(min); //3

要找到数组中最大值或者最小值,可以像下面这样使用apply()方法

1 var newArray = [1,2,3,6,5,4,8,9];2 var max =Math.max.apply(Math,newArray);3 console.log(max);

3、舍入方法

Math.ceil()执行向上舍入

Math.floor()执行向下舍入

Math.round()执行标准舍入

1 alert(Math.ceil(25.9));//26

2 alert(Math.ceil(25.5));//26

3 alert(Math.ceil(25.1));//26

4

5 alert(Math.floor(25.9));//25

6 alert(Math.floor(25.5));//25

7 alert(Math.floor(25.1));//25

8

9 alert(Math.round(25.9));//26

10 alert(Math.round(25.5));//26

11 alert(Math.round(25.1));//25

4、random()方法

返回大于等于0小于1的一个随机数

值 = Math.floor(Math.random() * 可能值的总数 + 第一个可能的值)

选择一个2到10之间的数值

1 var num = Math.floor(Math.random() * 9 + 2);2 console.log(num);

多数情况下,其实都可以通过一个函数来计算可能值的总数和第一个可能的值,例如

1 functionselectForm(lowerValue,upperValue){2 var choices = upperValue - lowerValue + 1;3 return Math.floor(Math.random() * choices +lowerValue);4 }5 var num = selectForm(2,10);6 console.log(num);7

8 //利用这个函数可以方便的从数组中随机的取出一项

9

10 var colors = ["red","green","blue","yellow","black","purple","brown"];11 var color = colors[selectForm(0,colors.length-1)];12 console.log(color);

5、其他方法

Math对象中还包含其他一些与完成各种简单或复杂计算有关的方法。见下表:

方  法

说  明

方  法

说  明

Math.abs(num)

返回num的绝对值

Math.asin(x)

返回x的反正弦值

Math.exp(num)

返回Math.E的num次幂

Math.atan(x)

返回x的反正切值

Math.log(num)

返回num的自然数

Math.atan2(y,x)

返回y/x反正切值

Math.pow(num,power)

返回num的power次幂

Math.cos(x)

返回x的余弦值

Math.sqrt(num)

返回num的平方根

Math.sin(x)

返回x的正弦值

Math.acos(x)

返回x的反余弦值

Math.tan(x)

返回x的正切值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值