2021-06-11 JS singleton built-in object - Global

Global object

brief introduction

The Global object is the most special object. ECMA-262 stipulates that the Global object is a bottom-up object and does not belong to the attributes and methods of any object. In fact, there is no such thing as Global variables or Global functions. Variables and functions defined in the Global scope become properties of the Global object.
isNaN(), isfinish(), parseInt(), and parseFloat(), are actually methods of Global objects. In addition to these, there are other methods on the Global object.

  • urL encoding method – encodeURI(), encodeuricomponent()
    🐖 Is uri 🙅‍♂️ url
    Both methods are used to encode uniform resource identifiers (URIs) so that they can be passed to the browser and understood. A valid URI cannot contain certain characters, such as spaces. Invalid characters need to be replaced.
    The encodeURI() method is used to encode the entire URI.
    The encodeURIComponent() method is used to encode individual components in a URI.
    The difference is that encodeURI() does not encode special characters belonging to URLL components, such as colons, slashes, question marks
    Pound sign, and encodeURIComponent() encodes all non-standard characters it finds.
let uri = "http://www.wrox.com/illegal value.js#start"; 

console.log(encodeURI(uri));   // "http://www.wrox.com/illegal%20value.js#start" 
console.log(encodeURIComponent(uri)); //"http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start" 

Generally speaking, encodeURIComponent() should be used more frequently than encodeURI(),
This is because the query string parameter is encoded more times than the base URI.

  • Decoding methods: decodeURI(), decodeURIComponent()
    decodeURI() decodes only characters encoded with encodeURI(). Similarly, decodeURIComponent() decodes all characters encoded by encodeURIComponent(), basically decoding all special values.
let uri = "http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.js%23start"; 

console.log(decodeURI(uri));  // http%3A%2F%2Fwww.wrox.com%2Fillegal value.js%23start 
console.log(decodeURIComponent(uri));  // http:// www.wrox.com/illegal value.js#start 
  • eval() method
    eval() is a complete ECMAScript interpreter that accepts a parameter, that is, an ECMAScript (JavaScript) string to be executed.
eval("console.log('hi')"); 
//The function of the above line of code is equivalent to that of the next line:
console.log("hi");

When the interpreter finds the eval() call, it will interpret the parameter as the actual ECMAScript statement, and then insert it into the location. The code executed through eval() belongs to the context of the call, and the executed code has the same scope chain as the context. This means that variables defined in the containment context can be referenced inside the eval() call.

let msg = "hello world"; 
eval("console.log(msg)");   // "hello world"

eval("function sayHi() { console.log('hi'); }"); 
sayHi();     // 'hi'

Any variables and functions defined through eval() are not promoted because they are contained in a string when parsing the code. They are created only when eval() executes.

eval("let msg = 'hello world';"); 
console.log(msg);    // Reference Error: msg is not defined

In strict mode, variables and functions created inside eval() cannot be accessed externally.

Note that the ability to interpret code strings is very powerful, but it is also very dangerous. When using eval(), you must
Be extremely cautious, especially when interpreting user input. Because this method will expose a great impact on XSS utilization
Attack surface. Malicious users may insert code that will crash your website or application.

  • window object
    Although ECMA-262 does not specify the way to directly access the Global object, the browser implements the window object as a proxy for the Global object.
var color = "red"; 
function sayColor() { 
 console.log(window.color); 
} 
window.sayColor(); // "red"

Another way to get Global objects:

let global = function(){
  return this;
}

Keywords: Javascript

Added by xydra on Sun, 30 Jan 2022 12:13:15 +0200