


JavaScript Advanced Programming (3rd Edition) Study Notes 2 js basic syntax_basic knowledge
This article reviews the basic syntax in the ECMAScript specification. Friends who are good at English can directly read the official document. JavaScript is essentially a C-like language. Friends who are familiar with C language can easily read this article or even skip it. However, it is recommended that you take a look. During the introduction, I may quote some It is a usage that I think is not easy to understand and is relatively popular.
Basic Grammar
1. Identifier: The so-called identifier actually refers to a name that meets certain specifications and can be recognized by the engine. It can be used to represent all nameable constants, variables, function names, function parameters, objects, object properties, etc. The name of the object.
(1) Case sensitive.
(2) Starts with a letter, underscore (_) or dollar sign ($), and other characters can be letters, underscores, dollar signs or numbers. The letters here contain extended ASCII or Unicode characters.
(3) Identifiers cannot be keywords, reserved words, true, false, or null. (Some browsers allow undefined, some don't).
(4) If the object attributes contain spaces or other special characters, they can be enclosed in parentheses as a whole.
2. Keywords: have specific uses in the language itself.
🎜> 3. Reserved words: reserved by the language itself and may be used as keywords in the future.
abstract boolean byte char class const debugger double enum export extends final float goto implementations import int interface long native package private protected public short static super syn chronized throws transient volatile
Reserved words in non-strict mode in ES5:
class const enum export extends import super
ES5 reserved words in strict mode:
implements interface let (new in ES5) package private protected public static yield (new in ES5)
4. Strict mode: Strict mode is introduced in ES5. Strict mode can be turned on by using "use strict". Global strict mode can be turned on at the top, or local strict mode can be turned on within the function scope.
5. Note: In ECMAScript, two types are supported Format comments, single-line comments and block-level comments:
Explanation: As JS code becomes more and more complex, comments become more and more important, and document automation becomes more and more important. Currently There are already many open source JS libraries used to automatically generate JS documents similar to Javadoc, such as JSDoc, YUIDoc, etc. At this time, there will also be corresponding format requirements for comments. Interested friends can find relevant information to study.
(1) Dynamic type: In ECMAScript, variables are dynamically typed. You can initialize them to a Number type when defining them. Then, you can assign a string value to it:
(2) var operator: Variables are declared using var. For uninitialized variables, they will default to undefined. You can also use variables directly without declaring them (in my opinion, this is also a feature that has no reason to exist. ), the most important difference between them is that when declared using var, the declared variable is only valid in the current scope, while when var is not used, the variable will be defined in the global scope. You can appreciate the difference through the following example:
var name = 'linjisong'; //Define global variables and assign values
age = 29; //Use variables directly, which is equivalent to defining global variables and assign values
//sal; //Error
var salary; / /Define global variables, not initialized
//This is just a function declaration, no actual call, so internally defined variables will not take effect
function fn(){
var name = 'oulinhai';//Definition Local variables and assign values
age = 23; //Assign values to global variables
work = 'it'; // Without using var, even variables local to the function will become global variables
}
//Before the function is actually called
console.info(salary); //undefined
console.info(name); // linjisong
console.info(age); // 29
try{
console.info(work);//Since work is not defined in the global environment, an exception will be thrown here
}catch(e){}
fn();//Actual call, Changes to variables in the code will appear
console.info(name); // linjisong, because var is used inside the function, the global name value will not be changed
console.info(age); // 23
console.info(work); // it
(3) Statement promotion: This issue will be discussed again when talking about function declarations and function expressions. Let’s mention it here first. , look at the code:
console.info(name);/ /undefined
console.info(getName);//getName() function
console.info(getName());//undefined
try{
console.info(age);// Exception
}catch(e){
console.info(e);//ReferenceError
}
console.info(getAge);//undefined
try{
console. info(getAge());//Exception
}catch(e){
console.info(e);//TypeError
}
var name = 'linjisong';//Variable declaration , raise
age = 29;//Use global variables directly, without raising
function getName(){//Function declaration, raise
return name;
}
var getAge = function( ){//Declaration of variable getAge, promoted; anonymous function expression to obtain age, not promoted
return age;
}
console.info(name);//linjisong
console.info (getName);//getName() function
console.info(getName());//linjisong
console.info(age);//29
console.info(getAge);// Anonymous function to get age
console.info(getAge());//29
Have you ever inferred the above output yourself? If you have already inferred it, you can skip it. If you still have questions, first take a look at the description of statement promotion below, and then go back and verify the above output results:
A. When the engine is parsing, it will first Parse the function declaration, then parse the variable declaration (the type will not be overwritten during parsing), and finally execute the code;
B. When parsing the function declaration, the type (function) will be parsed at the same time, but it will not be executed. When parsing the variable declaration, Only variables are parsed, not initialized.
What is involved here is only the global scope. There are also function parameters in the function scope that are also related to declaration promotion. We will discuss it later when we talk about functions.
The above code will first promote the function declaration on line 18 and the variable declaration on lines 16 and 21 to the beginning for parsing, and then execute it. Therefore, lines 1 and 9 output undefined because the variable declaration is promoted but not initialized, so line 11 throws a type exception because the function type cannot be determined; lines 2 and 3 are because the function declaration is promoted and the function type is parsed, so line 11 Line 2 outputs the function, line 3 can call the function, but the return value is not initialized and outputs undefined; line 5 will throw a reference exception because the variable has not been declared.
(4) You can use one statement to define multiple variables, just separate them with commas. For example:
var name='linjisong',
age=29,
work='it';
(5) In the strict mode of ES5, variables named eval or arguments cannot be defined.
7. Statement
(1) Statement: ends with a semicolon ";". If the semicolon is omitted, the parser determines the end of the statement.
I can’t think of any reason why statements in JS can omit semicolons. It is strongly recommended that each statement be clearly ended with a semicolon. Don’t let the parser spend time “guessing” your program. Moreover, More importantly, in many compression tools, guessing is not guaranteed to be 100% correct.
(2) Code block: Start with a left curly brace ({) and end with a right curly brace (}).
Although there is the concept of code block in JS, there is no corresponding block-level scope, which is different from general C-like languages. For control statements (such as if), don't avoid using code blocks just because there is only one statement. This will sow the seeds of mistakes for the people who maintain your program.
for(var i=0; i{
}
console.info(i);//Output 10, i can still be accessed after the code block, indicating that JS has no block-level scope
if(i //console.info(i); Without using code blocks, it is easy to make mistakes during maintenance (such as adding a statement)
{
console.info(i);
}
In addition to being used as code blocks, curly braces ({}) also have a very important use in defining object literals, which will be discussed later.

Understanding how JavaScript engine works internally is important to developers because it helps write more efficient code and understand performance bottlenecks and optimization strategies. 1) The engine's workflow includes three stages: parsing, compiling and execution; 2) During the execution process, the engine will perform dynamic optimization, such as inline cache and hidden classes; 3) Best practices include avoiding global variables, optimizing loops, using const and lets, and avoiding excessive use of closures.

Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.

Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.

The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.

Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.

JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.

I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same. First, what’s a multi-tenant SaaS application? Multi-tenant SaaS applications let you serve multiple customers from a sing

This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.