JavaScript全局对象

JavaScript provides a global object which has a set of properties, functions and objects that are accessed globally, without a namespace.

JavaScript提供了一个全局对象 ,该对象具有一组属性,函数和对象,这些属性,函数和对象可在没有命名空间的情况下进行全局访问。

The properties are:

这些属性是:

  • Infinity

    Infinity

  • NaN

    NaN

  • undefined

    undefined

The functions are:

这些功能是:

  • decodeURI()

    decodeURI()

  • decodeURIComponent()

    decodeURIComponent()

  • encodeURI()

    encodeURI()

  • encodeURIComponent()

    encodeURIComponent()

  • eval()

    eval()

  • isFinite()

    isFinite()

  • isNaN()

    isNaN()

  • parseFloat()

    parseFloat()

  • parseInt()

    parseInt()

These are the objects:

这些是对象:

and errors:

和错误:

  • Error

    Error

  • EvalError

    EvalError

  • RangeError

    RangeError

  • ReferenceError

    ReferenceError

  • SyntaxError

    SyntaxError

  • TypeError

    TypeError

  • URIError

    URIError

I describe errors on this JavaScript Errors reference post.

我在此JavaScript错误参考文章中描述了错误。

Let’s now describe here the global properties and functions.

现在让我们在这里描述全局属性和功能。

Infinity (Infinity)

Infinity in JavaScript is a value that represents infinity.

InfinityJavaScript是一个代表无限大的值。

Positive infinity. To get negative infinity, use the operator: -Infinity.

正无穷大。 要获得负无穷大,请使用运算符: -Infinity

Those are equivalent to Number.POSITIVE_INFINITY and Number.NEGATIVE_INFINITY.

这些等效于Number.POSITIVE_INFINITYNumber.NEGATIVE_INFINITY

Adding any number to Infinity, or multiplying Infinity for any number, still gives Infinity.

将任何数字添加到Infinity或将Infinity乘以任何数字,仍会得到Infinity

NaN (NaN)

The global NaN value is an acronym for Not a Number. It’s returned by operations such as zero divided by zero, invalid parseInt() operations, or other operations.

全局NaN值是Not a Number的首字母缩写。 它由诸如零除以零,无效的parseInt()操作或其他操作之类的操作返回。

parseInt()    //NaN
parseInt('a') //NaN
0/0           //NaN

A special thing to consider is that a NaN value is never equal to another NaN value. You must use the isNaN() global function to check if a value evaluates to NaN:

需要特别考虑的是,一个NaN值永远不会等于另一个NaN值。 您必须使用isNaN()全局函数来检查值是否为NaN

NaN === NaN //false
0/0 === NaN //false
isNaN(0/0)  //true

undefined (undefined)

The global undefined property holds the primitive value undefined.

全局undefined属性保存原始值undefined

Running a function that does not specify a return value returns undefined:

运行未指定返回值的函数将返回undefined

const test = () => {}
test() //undefined

Unlike NaN, we can compare an undefined value with undefined, and get true:

NaN不同,我们可以将undefined值与undefined进行比较,然后得出true:

undefined === undefined

It’s common to use the typeof operator to determine if a variable is undefined:

通常使用typeof运算符来确定变量是否未定义:

if (typeof dog === 'undefined') {

}

decodeURI() (decodeURI())

Performs the opposite operation of encodeURI()

执行encodeURI()的相反操作

decodeURIComponent() (decodeURIComponent())

Performs the opposite operation of encodeURIComponent()

执行encodeURIComponent()的相反操作

encodeURI() (encodeURI())

This function is used to encode a complete URL. It does encode all characters to their HTML entities except the ones that have a special meaning in a URI structure, including all characters and digits, plus those special characters:

此功能用于编码完整的URL。 它确实将所有字符编码为它们HTML实体,但在URI结构中具有特殊含义的字符除外,包括所有字符和数字,以及那些特殊字符:

~!@#$&*()=:/,;?+-_.

~!@#$&*()=:/,;?+-_.

Example:

例:

encodeURI("http://flaviocopes.com/ hey!/")
//"http://flaviocopes.com/%20hey!/"

encodeURIComponent() (encodeURIComponent())

Similar to encodeURI(), encodeURIComponent() is meant to have a different job.

encodeURI()类似, encodeURIComponent()意味着具有不同的工作。

Instead of being used to encode an entire URI, it encodes a portion of a URI.

它不是编码整个URI,而是编码URI的一部分。

It does encode all characters to their HTML entities except the ones that have a special meaning in a URI structure, including all characters and digits, plus those special characters:

它确实将所有字符编码为它们HTML实体,但在URI结构中具有特殊含义的字符除外,包括所有字符和数字,以及那些特殊字符:

-_.!~*'()

-_.!~*'()

Example:

例:

encodeURIComponent("http://www.example.org/a file with spaces.html")
// "http%3A%2F%2Fwww.example.org%2Fa%20file%20with%20spaces.html"

eval() (eval())

This is a special function that takes a string that contains JavaScript code, and evaluates / runs it.

这是一个特殊功能,它接受包含JavaScript代码的字符串,并对其进行评估/运行。

This function is very rarely used and for a reason: it can be dangerous.

此功能很少使用,原因如下:它可能很危险。

I recommend to read this article on the subject.

我建议阅读有关该主题的这篇文章

isFinite() (isFinite())

Returns true if the value passed as parameter is finite.

如果作为参数传递的值是有限的,则返回true。

isFinite(1)                        //true
isFinite(Number.POSITIVE_INFINITY) //false
isFinite(Infinity)                 //false

isNaN() (isNaN())

Returns true if the value passed as parameter evaluates to NaN.

如果作为参数传递的值的值为NaN则返回true。

isNaN(NaN)        //true
isNaN(Number.NaN) //true
isNaN('x')        //true
isNaN(2)          //false
isNaN(undefined)  //true

This function is very useful because a NaN value is never equal to another NaN value. You must use the isNaN() global function to check if a value evaluates to NaN:

此功能非常有用,因为一个NaN值永远不会等于另一个NaN值。 您必须使用isNaN()全局函数来检查值是否为NaN

0/0 === NaN //false
isNaN(0/0)  //true

parseFloat() (parseFloat())

Like parseInt(), parseFloat() is used to convert a string value into a number, but retains the decimal part:

parseInt()类似, parseFloat()用于将字符串值转换为数字,但保留小数部分:

parseFloat('10,000', 10) //10     ❌
parseFloat('10.00', 10)  //10     ✅ (considered decimals, cut)
parseFloat('10.000', 10) //10     ✅ (considered decimals, cut)
parseFloat('10.20', 10)  //10.2   ✅ (considered decimals)
parseFloat('10.81', 10)  //10.81  ✅ (considered decimals)
parseFloat('10000', 10)  //10000  ✅

parseInt() (parseInt())

This function is used to convert a string value into a number.

此函数用于将字符串值转换为数字。

Another good solution for integers is to call the parseInt() function:

整数的另一个好的解决方案是调用parseInt()函数:

const count = parseInt('1234', 10) //1234

Don’t forget the second parameter, which is the radix, always 10 for decimal numbers, or the conversion might try to guess the radix and give unexpected results.

不要忘记第二个参数,即基数,十进制数字始终为10,否则转换可能会尝试猜测基数并给出意外结果。

parseInt() tries to get a number from a string that does not only contain a number:

parseInt()尝试从不仅包含数字的字符串中获取数字:

parseInt('10 lions', 10) //10

but if the string does not start with a number, you’ll get NaN (Not a Number):

但是如果字符串不是以数字开头,则会得到NaN (不是数字):

parseInt("I'm 10", 10) //NaN

Also, just like Number it’s not reliable with separators between the digits:

另外,就像Number一样,数字之间用分隔符也不可靠:

parseInt('10,000', 10) //10     ❌
parseInt('10.00', 10)  //10     ✅ (considered decimals, cut)
parseInt('10.000', 10) //10     ✅ (considered decimals, cut)
parseInt('10.20', 10)  //10     ✅ (considered decimals, cut)
parseInt('10.81', 10)  //10     ✅ (considered decimals, cut)
parseInt('10000', 10)  //10000  ✅

翻译自: https://flaviocopes.com/javascript-global-object/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值