JavaScript basic syntax

Source: Internet
Author: User

0x00: Cause

First of all, I learned the original intention of JavaScript, I am currently engaged in the security industry, can now tap some of the basic XSS vulnerabilities, but want to understand the XSS, need to have javascript basis, such as understanding the DOM type XSS, so there is such a out, the following is the record their own study notes.
JavaScript is a simple language and a complex language. It's simple because it takes a moment to learn to use it, and it's complicated because it takes years to really master it. In fact, the front-end engineer is largely referred to as a JavaScript engineer. Getting started with the front end is easy to master, and it's about JavaScript. This article is the first of the basic JavaScript syntax-the lexical structure. Lexical constructs are a set of basic rules that describe how to use JavaScript to write programs

0x01: Relationship with Java

About JavaScript There is a saying that the relationship between Java and JavaScript is Lei Feng and Lei Feng Tower. Does that really matter?

The first name for JavaScript was LiveScript, and then the reason for choosing JavaScript as its official name was probably to make it sound like a famous door. In addition to the syntax that looks like Java, JavaScript and Java are two completely different programming languages

The programming language is divided into two categories: explanatory type and compiler type. Languages such as Java or C + + require a compiler. A compiler is a program that translates source code written in high-level languages such as Java into files that are executed directly on a computer. Interpreted programming languages do not require compilers-they only need an interpreter, and the JavaScript interpreter in the browser is read directly into the source code and executed

Java can theoretically be deployed in almost any environment, but JavaScript tends to apply only to Web browsers. Furthermore, in the JavaScript language, functions are a separate type of data, using the inheritance chain based on the prototype object (prototype), which is much more free of JavaScript syntax than Java

Basically, the name JavaScript is meant to be "very much like the scripting language of Java."

Defined
JavaScript is a dynamic, weakly-typed, interpreted programming language that is ideal for object-oriented and functional programming styles. JavaScript's syntax derives from Java, its class-a function comes from scheme, and its prototype-based inheritance comes from self

JavaScript is used to enhance the dynamic effect of the page, enabling real-time and dynamic interaction between pages and users.

JavaScript consists of three parts: ECMAScript, Dom, and BOM

[1] ECMAScript is defined by ECMA-262 and provides core language functionality (ECMA is the European Association of Computer Manufacturers)

[2] DOM Document Object Model, which provides methods and interfaces for accessing and manipulating Web page content

[3] BOM (Browser object model) browser objects models that provide methods and interfaces for interacting with the browser

0x02: Case Sensitive

In the language of JavaScript, it is not too much to emphasize that the character is case sensitive. The keywords, variables, function names, and all identifiers in JavaScript must be in a consistent case form

`//‘online‘、‘Online‘、‘OnLine‘、‘ONLINE‘是四个不同的变量名`

Note HTML is not case sensitive (although XHTML is case-sensitive). Many client-side JavaScript objects and properties have the same name as the HTML tags and attributes they represent. In HTML, these tags and property names can be capitalized or lowercase, while in JavaScript they must be lowercase. For example, when you set an event handler in HTML, the OnClick property can be written as an onclick, but in JavaScript code, you must use the lower-case onclick

0x03: Reserved word (Reservedword)

Like any other programming language, JavaScript retains some identifiers for its own use. These reserved words cannot be used as normal identifiers. Because a lot of reference books misleading, seemingly reserved words and keywords are separate, in fact, is not, the keyword is only a part of the reserved word. Reserved words include keywords, future reserved words, empty literals, and Boolean literals

Reserved words

 Reservedword:: Keyword futurereservedword nullliteral booleanliteral ' * * keyword * * break            Do instanceof typeof Case Else new Var catch finally return    Void continue for switch while debugger function this with default       If throw delete in try** future reserved Words * * The following words are used as suggested extension keywords, so they are retained so that these extended class enums may be used in the future Extends Super Const EXPORT IMPORT**ECMASCRIPT3 version * * Above is a reserved word for ECMAScript5, but the reserved word in the ECMASCRIPT3 version is not the same, if you want the code to be in the base When running on an interpreter implemented by ECMASCRIPT3, you should avoid using the following reserved words as identifiers abstract Boolean byte char class constdouble enum export extends final Floatgoto Implements import int Interfacelong native package private protectedpublic short static super synchronized throw Transien T volatile # 0x04: predefined variables and function JavaScript pre-defined many global variables and functions, and should avoid having their names used as identifier names ' arguments Array Boolean Date decodeURI decodeURIComponent EncodeuriencodeuricoMponent Error eval evalerror Function Infinity isfinite isNaN JSON Math NaN number Object parsefloat parseint rangeerroref Erenceerror RegExp String syntaxerror TypeError undefined urierror ' # 0x05: note (Comment) Not all statements need to be interpreted and executed by the JavaScript interpreter. Sometimes it is necessary to write some information in the script that is only for your own reference or to remind yourself, and you want the JavaScript interpreter to ignore this information directly, such as comments can effectively help understand the code flow, in the code they play the role of the note in life, can help us figure out what the script did [ Note] Note that it is important to describe the code accurately, there are many ways to insert comments in a JavaScript script, including single-line comments, multiline comments, and HTML-style comments "1" single-line comments with two slashes starting with "//single-line comment" 2 ", which is worse than no comment Multiline comments are also called block-level comments, starting with a slash and an asterisk/*, ending with an asterisk and a slash/*

/
This is a multi-line comment
/

  [注意]块级注释/**/可以跨行书写,但不能嵌套,否则会报错

Error
/
Note 1
/

Note 1.1
/
/

[注意]块级注释/**/中的那些字符也可能出现在正则表达式字面量里,所以块级注释对于被注释的代码块来说是不安全的/*    var rm_a = /a*/.match(s);*/【3】HTML风格的注释仅仅适用于单行注释,其实javascript解释器对<!--的处理和对//的处理是一样的<!-- 这是javascript中的注释  如果在HTML文档中,还需要以-->来结束注释`<!-- 这是HTML中的注释 -->`  但javascript不要求这么做,它会把-->视为注释内容的一部分  [注意]HTML允许上面这样的注释跨越多行,但这种注释的每行都必须在开头加上"<!--"来作为标志`<!-- 我是注释1<!-- 我是注释2<!-- 我是注释3`  因为javascript解释器在处理这种风格的注释时与HTML做法不同,为避免发生混淆,最好不要在javascript脚本中使用HTML风格的注释**空白(WhiteSpace)**  空白通常没有意义,有时候必须要用它来分隔字符序列,否则它们就会被合并成一个符号var that = this;  var和that之间的空白是不能移除的,但其他的空白可以移除  javascript会忽略程序中标识(token)之间的空格。多数情况下,javascript同样会忽略换行符。由于可以在代码中随意使用空格和换行,因此可以采用整齐、一致的缩进来形成统一的编码风格,从而提高代码的可读性//通过增加空白字符,提高代码可读性for(var i = 1; i < 10; i++){    //}  javascript将如下这些识别为空白字符WhiteSpace

\u0009 horizontal tab <TAB>
\u000b vertical tab <VT>
\u000c page break <ff>
\u0020 whitespace <sp>
\u00a0 Non-disruptive whitespace <nbsp>
\ufeff word Fu She tag

  JavaScript recognizes the following characters as line Terminator lineterminator\u000a newline character <lf>\u000d carriage return <cr>\u2028 line delimiter &L T ls>\u2029 Paragraph separator <PS> optional semicolon JavaScript uses semicolons, separating statements, which is important for enhancing the readability and cleanliness of your code, where you can omit semicolons, and in some places you cannot omit semicolons//two statements to write in two lines, A semicolon can omit a = 3;b = 4;//Two statements written in one line, the first semicolon cannot omit a = 3;  b = 4; But JavaScript does not fill the semicolon at all line breaks, and JavaScript fills the semicolon only if the code is not parsed correctly when the semicolon is missing. In other words, if the current statement and subsequent non-whitespace characters cannot be parsed as a whole, JavaScript fills in the semicolon at the end of the current statement line, Var aa=3console.log (a) javascript resolves it to: var a;a = 3;  Console.log (a); The separation rules for this statement cause some unexpected scenarios var y = x + f (a+b). tostring javascript resolves it to: var y = x + f (a+b). ToString Therefore, in order for the above code to parse into two different statements, you must manually fill in the An explicit semicolon at the end of a line generally speaking, if a statement starts with a symbol of ' (', ' [', '/', ' + ', '-'), then it is most likely to resolve the two exceptions together with the previous statement if the current statement and the next line of statements cannot be parsed, JavaScript fills the semicolon after the first line, This is a general rule, but there are two exceptions for "1" The first exception is in a scene involving a return, break, continue, throw statement.  If these four keywords are followed by a newline, JavaScript fills the semicolon returntrue at the line break;  JavaScript interprets it as: return;true;  And the code is meant to be: return true; "2" The second exception is when the + + and--operators are involved, and if used as a suffix expression, it should be the same line as the expression.  Otherwise, the end of the line will fill the semicolon, while + + or--will act as the prefix operator for the next line of code and parse X++y JavaScript with it to parse it as: x;++y; and the CodeThe original intention is: X++;y; Although a semicolon is not required, it is best not to omit it, because a semicolon avoids many errors, and there is no semicolon at the end of the line of code to cause a compression error. Plus semicolons also improve the performance of your code in some cases, because it eliminates the need for the parser to take the time to speculate on where to insert the semicolon. Reference "1" es5/lexical https://www.w3.org/html/ig/zh/wiki/ES5/lexical "2" Nanyi JavaScript Standard Reference Tutorial--Grammar Overview http://javascript.ruanyifeng.com/grammar/basic.html "3" W3school-javascript advanced Tutorial-- ECMAScript Grammar http://www.w3school.com.cn/js/pro_js_syntax.asp "4", JavaScript authoritative Guide (6th edition), chapter 2nd lexical Structure "5", advanced programming of JavaScript (3rd Edition) 3rd Chapter basic Concept "6" JavaScript Language Essence (Revised edition) 2nd Chapter Syntax "7" JavaScript DOM Programming Art (2nd Edition) "2nd Chapter JavaScript Syntax"

JavaScript basic Syntax

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.