Basic JavaScript (mandatory) and javascript

Source: Internet
Author: User

Basic JavaScript (mandatory) and javascript

It takes a long time to get started with the JavaScript language, but it has never been understood by the system. While I have just graduated and have no intention of working for some reasons, I want to use this language to develop my blog writing habits, because I think this is a sacred and glorious thing for programmers.

1.1 Background

I believe that many beginners forget or confuse the official name of JavaScript: ECMAScript. On March 6, June 17, 2015, ECMAScript 6 was officially released, namely ECMAScript 2015.

1.2 syntax

General syntax omitted

Emphasis:

1. Original values and objects: the original values include boolean values, numbers, strings, null values, and undefined values. All other values are objects. The main difference between the two lies in their comparison method: each object has a unique identifier and is only equal to itself.

var obj1={};var obj2={};alert(obj1 === obj2);//falsealert(obj1===obj1);//truevar prim1=123;var prim2=123;alert(prim1===prim2);//true

2. Use typeof and instanceof pair value classification.

Typeof

Operands Result
Undefined 'Undefined'
Null Object
Boolean Value Boolean
Number Number
String String
Function Function
All other common values Object
Value created by the engine The JS engine can be used to create some values, and the typeof results can return any string.
   

3. boolean value:

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

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

4. IIFE:

Introduce a new scope. Purpose: Remove unintentional sharing caused by closures (functions and variables in the surrounding scopes connected to them.

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)

The return value of this line marked as (1) is always the current value of I, rather than the value when the function is created. After the loop ends, the value of I is 5, so all functions in the array return this value. If you want to mark (1) this row of functions to get 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});      }()) ; }

This is part of the knowledge that you did not pay attention to or did not know during the sorting process. It is written here to supplement the knowledge points.

The above JavaScript BASICS (mandatory) are all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support the help house.

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.