The window object is the ultimate bottom-up object for JavaScript in web browsers. It is at the end of the scope and is an object that contains all 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. Quote
There are usually two ways to reference the window object. The first is to simply reference the name given to the window object; the second is to use the this keyword in the global scope
var foo ='bar'; windowRef1 = window; windowRef2 = this; console.log(windowRef1,windowRef2);//输出window对象的引用 console.log(windowRef1.foo,windowRef2.foo);//'bar' 'bar'
2. Features
The window object is implicit and usually not referenced explicitly; 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()和alert()语句基本上是相同的 var foo = {//window对象在这里是隐式的,window.foo fooMethod: function(){ alert('foo' + 'bar');//window对象在这里是隐式的,window.alert window.alert('foo' + 'bar');//显式调用window对象,效果一样 } } foo.fooMethod();//window对象在这里是隐式的,window.foo.fooMethod()
3. Attributes
The window object has a total of 18 attributes
undefined NaN Infinity Boolean String Number Object Array Function Date RegExp Error EvalError RangeError ReferenceError SyntaxError TypeError URIError
[Note] Assigning values to undefined, NaN and Infinity is prohibited
4. Method
Javascript comes with some predefined functions, which are considered methods of the window object
1), encoding method
encodeURI():Encode the entire URI, replacing all invalid characters with special UTF-8
There are 82 unencoded characters in encodeURI():
! # $ & ' ( ) * + , - . / : ; = ? @ _ ~ 0-9 a-z A-Z
encodeURIComponent():Encode a certain section of the URI (commonly used for passing parameters in the GET method), replacing all invalid characters with special UTF-8
Generally speaking, use encodeURIComponent() more than encodeURI() because in practice it is more common to query string parameters rather than encode the underlying URI. There are 71 unencoded characters in encodeURIComponent():
! ' ( ) * - . _ ~ 0-9 a-z A-Z
escape():Encode the string and convert the unicode encoding of the character into a hexadecimal sequence
The use of escape() is opposed in ES3, and it is recommended to use encodeURI and encodeURIComponent instead. However, escape() is still widely used for cookie encoding, because escape() happens to encode illegal characters in cookies and is often used in paths. Appearances of "/" are not encoded. There are 69 unencoded characters for escape():
* + - . / @ _ 0-9 a-z A-Z
decodeURI():decode encodeURI()
decodeURIComponent():decode encodeURIComponent()
unescape():Decode 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 complete ECMAScript parser, accepting only one parameter, the JavaScript string to be executed. When the parser finds that the eval() method is called in the code, it will parse the incoming parameters as actual ECMAScript statements, and then insert the execution results into the original position. The eval() method's ability to interpret strings is very powerful, but also very dangerous. When it is used to perform user input data, a malicious user may enter code that threatens the site or application characters, which is called code injection
[Note] In strict mode, any variables or functions created in eval() cannot be accessed externally, and assigning a value to eval will also cause an error
2), digital method
There are four digital methods under the Window object: isFinite(), isNaN(), parseFloat(), and parseInt()
The above is the entire content of this article, I hope it will be helpful to everyone’s study.