Home  >  Article  >  Web Front-end  >  Detailed explanation of javascript window object references, characteristics, properties, and methods

Detailed explanation of javascript window object references, characteristics, properties, and methods

伊谢尔伦
伊谢尔伦Original
2017-07-26 11:04:411929browse

The window object is the ultimate bottom-up object of 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

1. Reference

There are usually two types of references to window Object methods. 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, characteristics

The window object is implicit and is 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. Properties
The window object has a total of 18 properties


##

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

[Note] Prohibited Assign values ​​to undefined, NaN and Infinity

4. Methods

Javascript comes with some predefined functions, which are considered to be methods of the window object

1), encoding method

encodeURI():Encode the entire URI, using special UTF -8 Replace all invalid characters

encodeURI() has 82 unencoded characters:

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

Generally speaking, use encodeURIComponent() more than encodeURI() because in practice it is more common to query string parameters rather than to the underlying URI encoding. There are 71 unencoded characters in encodeURIComponent():

! ' ( ) * - . _ ~ 0-9 a-z A-Z
escape():Encode the string and convert the unicode encoding of the characters into a hexadecimal sequence

The use of escape() is opposed in ES3, and it is recommended to use encodeURI and encodeURIComponent Instead, escape() is still widely used for cookie encoding, because escape() happens to encode illegal characters in cookies and does not encode the "/" that often appears in the path. There are 69 unencoded characters for escape():

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

decodeURIComponent():Decode encodeURIComponent()

unescape():Decode escape()

eval() The eval() method is like a complete The ECMAScript parser accepts 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 the so-called code injection

[Note] In strict mode, eval() is not accessible from the outside. For any variable or function created in, assigning a value to eval will also cause an error

2), digital method There is also isFinite under the Window object (), isNaN(), parseFloat(), parseInt(), these four methods about numbers

The above is the detailed content of Detailed explanation of javascript window object references, characteristics, properties, and methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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