Home > Web Front-end > JS Tutorial > body text

JavaScript basics (must read)

高洛峰
Release: 2017-01-21 09:55:17
Original
1153 people have browsed it

I have been exposed to JavaScript for a long time, but I have never systematically understood such a language. I have just graduated and I am not interested in working for some reasons. I want to systematically understand this language. I also want to develop the habit of blogging through this language, because I think this is a very sacred and glorious thing for programmers. matter.

1.1 Background

I believe that what many beginners have forgotten or confused is the official name of JavaScript: ECMAScript. On June 17, 2015, ECMAScript 6 released the official version, ECMAScript 2015.

1.2 Grammar

General syntax omitted

Emphasis on:

1. Original values ​​and objects: Original values ​​include Boolean values, numbers, strings, null, and undefined. All other values ​​are objects. The main difference between the two is how they compare: each object has a unique identity and is only equal to itself.

var obj1={};
var obj2={};
alert(obj1 === obj2);
 
//false
 
alert(obj1===obj1);
 
//true
 
var prim1=123;
var prim2=123;
alert(prim1===prim2);
 
//true
Copy after login

2. Use typeof and instanceof to classify values.

typeof

JavaScript basics (must read)

3.Boolean value:

False value: undefined,null,false,-0,NaN,''

Binary logical operators: Binary logical operators in JavaScript are short-circuited. If the first operand is sufficient to determine the result, the second operand is not evaluated. And (&&): If the first operand is false, return it. or (||): If the first operand is true, return it.

4.IIFE:

Introducing a new scope. Effect: Remove unintentional sharing caused by closures (functions and variables in the surrounding scopes to which it is connected).

Example:

var result=[];
for(var i=0;i<5;i++)
{
result.push(function(){return i;});//(1)
}
console.log(result[1]()); //5  (not 1)
console.log(result[3]()); //5  (not 3)
Copy after login

The return value of the line marked (1) is always the current value of i, not the value when the function was created. After the loop ends, the value of i is 5, so all functions in the array return this value. If you want the function in the line marked (1) to obtain a snapshot of the current i value, you can use IIFE.

for(var i=0;i<5;i++)
{
 (function (){
  var i2=i;
  result.push(function(){return i2});    
 }()
) ;
}
Copy after login

The above are some of the knowledge that you have not noticed or learned before during the sorting process. I write it here to supplement the knowledge points.

The above-mentioned JavaScript basics (must read) are all the content shared by the editor. I hope it can give you a reference, and I hope you will support the PHP Chinese website.

For more JavaScript basics (must-read) related articles, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!