javascript type system Window object learning notes

  • 2020-11-25 07:06:00
  • OfStack

The window object is the ultimate pocket object of javascript in the Web browser, at the end of the scope, and an object that contains all the objects. All properties and functions defined in the global scope are properties of the window object


var myStringVar = 'myString';
var myFunctionVar = function(){};
console.log('myStringVar' in window);//true
console.log('myFunctionVar' in window);//true

1, references,

There are usually two ways to reference window objects. The first is a simple reference to the name given to the window object; The second is to use the this keyword in a global scope


var foo ='bar';
windowRef1 = window;
windowRef2 = this;
console.log(windowRef1,windowRef2);// The output window Object reference 
console.log(windowRef1.foo,windowRef2.foo);//'bar' 'bar' 

2, features,

window objects are implicit and are usually not explicitly referenced; Even if the window object is explicitly declared, it is implicit because the window object is the last one in the scope chain


//window.alert() and alert() The statements are essentially the same 
var foo = {//window The object here is implicit, window.foo
 fooMethod: function(){
  alert('foo' + 'bar');//window The object here is implicit, window.alert
  window.alert('foo' + 'bar');// Explicitly call window Object, effect 1 sample 
 }
}
foo.fooMethod();//window The object here is implicit, window.foo.fooMethod()


3, attributes,
The window object has 18 attributes


undefined NaN Infinity 
Boolean String Number Object Array Function Date RegExp 
Error EvalError RangeError ReferenceError SyntaxError TypeError URIError 

[Note] Assignments to undefined, NaN, and Infinity are prohibited

4, methods,

Javascript comes with 1 predefined functions that are considered methods of window objects

1) Coding method

encodeURI(): Encodes the entire URI, replacing all invalid characters with the special ES47en-8

There are 82 unencoded characters in encodeURI() :

! # $ & ' ( ) * + , - . / : ; = ? @ _ ~ 0-9 a-z A-Z
encodeURIComponent(): Encodes a segment of URI (often used to pass parameters to the GET method), replacing all invalid characters with the special UTF-8

In general, encodeURIComponent() is used more than encodeURI() because in practice it is more common to query string parameters than to encode the base URI. There are 71 unencoded characters in encodeURIComponent() :

! ' ( ) * - . _ ~ 0-9 a-z A-Z
escape(): Encodes a string to convert the unicode encoding of a character to a hexadecimal sequence

ES3 opposes the use of escape() and suggests encodeURI and encodeURIComponent instead, but escape() is still widely used for cookie encoding because escape() happens to encode the illegal characters in cookie and does not encode the "/" that often appears in the path. There are 69 unencoded characters in escape() :

* + - . / @ _ 0-9 a-z A-Z
decodeURI () : decoding encodeURI ()

decodeURIComponent () : decoding encodeURIComponent ()

unescape () : decoding escape ()


var uri = "http://www.wrox.com/illegal value.htm#start";
console.log(encodeURI(uri));//http://www.wrox.com/illegal%20value.htm#start
console.log(encodeURIComponent(uri));//http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start
console.log(escape(uri));//http%3A//www.wrox.com/illegal%20value.htm%23start
var uri = 'http%3A%2F%2Fwww.wrox.com%2Fillegal%20value.htm%23start';
console.log(decodeURI(uri));//http%3A%2F%2Fwww.wrox.com%2Fillegal value.htm%23start
console.log(decodeURIComponent(uri));//http://www.wrox.com/illegal value.htm#start
console.log(unescape(uri));//http://www.wrox.com/illegal value.htm#start

eval()

The eval() method is like a full ECMAScript parser, accepting only one argument, the JavaScript string to be executed. When the parser finds that the eval() method is called in the code, it parses the passed arguments as the actual ECMAScript statement, and then inserts the result of the execution into the original location. The eval() method's ability to interpret strings is powerful, but it's also dangerous. When it is used to perform user input data, there may be malicious users entering code that threatens the site or application characters, known as code injection

[Note] In strict mode, any variables or functions created in eval() cannot be accessed externally, and assigning eval also causes an error

2) Numerical method

Window object isFinite(), isNaN(), parseFloat(), parseInt(), parseInt() under the four number methods

Above is the entire content of this article, I hope to help you with your study.


Related articles: