Home Web Front-end JS Tutorial Javascript Global Object_Basic Knowledge

Javascript Global Object_Basic Knowledge

May 16, 2016 pm 06:48 PM
global javascript

Global对象

Global对象是ECMAScript中最特别的对象,因为实际上它根本不存在。如果尝试编写下面的代码,将得到错误:

错误消息显示Global不是对象,但刚才不是说Global是对象吗?没错。这里需要理解的主要概念是,在ECMAScript中,不存在独立的函数,所有函数都必须是某个对象的方法。本书前面介绍的函数,如isNaN()、isFinite()、parseInt()和parseFloat()等,看起来都像独立的函数。实际上,它们都是Global对象的方法。而且Global对象的方法不止这些。

encodeURI()和encodeURIComponent()方法用于编码传递给浏览器的URI(统一资源标识符)。有效的URI不能包含某些字符,如空格。这两个方法用于编码URI,这样用专门的UTF-8编码替换所有的非有效字符,就可以使浏览器仍能够接受并理解它们。

encodeURI()方法用于处理完整的URI(例如,http://www.wrox.com/illegal value.htm),而encodeURIComponent()用于处理URI的一个片断(如前面的URI中的illegal value.htm)。这两个方法的主要区别是encodeURI()方法不对URI中的特殊字符进行编码,如冒号、前斜杠、问号和英镑符号,而encodeURIComponent()则对它发现的所有非标准字符进行编码。例如:

这段代码输出两个值:

可以看到,除空格外,第一个URI无任何改变,空格被替换为%20。第二个URI中的所有非字母数字字符都被替换成它们对应的编码,基本上使这个URI变得无用。这就是encodeURI()可以处理完整URI,而encodeURIComponent()只能处理附加在已有URI末尾的字符串的原因。

自然,还有两个方法用于解码编码过的URI,即decodeURI()和decodeURIComponent()。如你所料,这两个方法所做的恰与其对应的方法相反。decodeURI()方法只对用encodeURI()方法替换的字符解码。例如,%20将被替换为空格,而%23不会被替换,因为它表示的是英镑符号(#),encodeURI()并不替换这个符号。同样的,decodeURIComponent()会解码所有encodeURIComponent()编码过的字符,意味着它将对所有的特殊值解码。例如:

这段代码输出两个值:

在这个例子中,变量uri存放的是用encodeURIComponent()编码的字符串。生成的值说明了应用两个解码方法时会发生的事情。第一个值由decodeURI()输出,把%20替换成空格。第二个值由decodeURIComponent()输出,替换所有的特殊。

这些URI方法encodeURI()encodeURIComponent()decodeURI()decodeURICom- ponent()代替了BOM的escape()unescape()方法。URI方法更可取,因为它们会对所有Unicode符号编码,而BOM方法只能对ASCII符号正确编码。尽量避免使用escape()unescape()方法。

The last method is probably the most powerful method in the entire ECMAScript language, the eval() method. This method is like the entire ECMAScript interpreter, accepting one parameter, which is the ECMAScript (or JavaScript) string to be executed. For example:

This line of code is functionally equivalent to the following code:

When the interpreter finds an eval() call, it will interpret the argument as a real ECMAScript statement and insert it where the function is. This means that variables referenced inside the eval() call can be defined outside of the parameters:

Here, the variable msg is defined outside the context of the eval() call, and the warning still displays the text "hello world" because the second line of code will be replaced with a real line of code. Likewise, a function or variable can be defined inside an eval() call and then referenced in code outside the function:

Here, the function sayHi() is defined inside the eval() call. Because the call will be replaced with a real function, sayHi() can still be called on the next line.

This function is very powerful, but also very dangerous. Be extremely careful when using eval(), especially when passing it user-entered data. A malicious user may insert values ​​that compromise the security of the site or application (called code injection).

Global object not only has methods, it also has properties. Remember those special values ​​undefined, NaN and Infinity? They are all properties of the Global object. In addition, the constructors of all local objects are also properties of the Global object. The following table explains all properties of the Global object in more detail:

Attributes

属    性

说    明

undefined

Undefined类型的字面量

NaN

非数的专用数值

Infinity

无穷大值的专用数值

Object

Object的构造函数

Array

Array的构造函数

Function

Function的构造函数

Boolean

Boolean的构造函数

String

String的构造函数

Number

Number的构造函数

Date

Date的构造函数

RegExp

RegExp的构造函数

Error

Error的构造函数

EvalError

EvalError的构造函数

RangeError

RangeError的构造函数

ReferenceError

ReferenceError的构造函数

SyntaxError

SyntaxError的构造函数

TypeError

TypeError的构造函数

URIError

URIError的构造函

Say Ming

undefined Literal of Undefined type
NaN Special numerical values ​​that are not numbers
Infinity Special numerical value for infinity
Object Constructor of Object
Array Constructor of Array
Function Constructor of Function
Boolean Boolean constructor
String Constructor of String
Number Number’s constructor
Date Date’s constructor
RegExp Constructor of RegExp
Error Constructor of Error
EvalError Constructor of EvalError
RangeError Constructor of RangeError
ReferenceError Constructor of ReferenceError
SyntaxError Constructor of SyntaxError
TypeError Constructor of TypeError
URIError Constructor of URIError
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to implement an online speech recognition system using WebSocket and JavaScript How to implement an online speech recognition system using WebSocket and JavaScript Dec 17, 2023 pm 02:54 PM

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

How to implement an online reservation system using WebSocket and JavaScript How to implement an online reservation system using WebSocket and JavaScript Dec 17, 2023 am 09:39 AM

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

How to use JavaScript and WebSocket to implement a real-time online ordering system How to use JavaScript and WebSocket to implement a real-time online ordering system Dec 17, 2023 pm 12:09 PM

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

How to use insertBefore in javascript How to use insertBefore in javascript Nov 24, 2023 am 11:56 AM

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).

See all articles