JS composition and variable

JavaScript variables and data types

Js do client language

According to Js grammar, to operate the relevant elements in the page, and sometimes operate some of the features inside the browser

Js consists of three parts:

  • ECMAScript( ES): Describes the syntax of the language and basic object
  • DOM( Document Object Model): Document Object Model, the contents of the page description of the processing methods and interfaces through a number of Jsproperties and methods for manipulating the page DOMelement
  • BOM( Browser Object Model): Browser object model, describing browser method and interactive interface, for operating the browser.

Js variables in Variable

JS data divided into two categories: First, the basic data types: Undefined, , Null, Boolean, Number, String, and Symbolsecond, the data type is: Object, Objectessentially by a set of unordered's name pairs.

Variable: a variable amount, in a programming language, is actually a variable name, used to store things and represent different values.

// ES3
var a = 123;
// ES6
let b = 100;
b = 200 //报错
const c = 1000;


// 创建函数也相当于在创建变量
function fn() {}

// 创建类也相当于创建变量
class A{}

// ES6的模块岛屿也可以创建变量
import a from './a.js'

// Symbol创建唯一值
let n = Symbol(100)
let m = Symbol(100)

to sum up:

  • With varvariable declaration, the declaration may be repeated after the override earlier statement
  • Use lethas scoped variables declared in the statement can not be repeated under the same scope variables, but you can modify the value of a variable, for example let a = 1;, can not be declared again a, let a = 2;you will get an error, suggesting:Uncaught SyntaxError: Identifier 'a' has already been declared
  • With constvariable declaration is constant, not modify the value of the variable, but as Objectcomplex data types such, stored in memory is a pointer to the object, that is to say, with conststill modify the value of the target object declaration but not declare the object again

The data type JS

  • Basic data types
    • Digital Number(digital and conventional NaN)

    • String String
    • Boolean value Boolean
    • Null pointer Null
    • Undefined Undefined
    • Unique value (unique value)Symbol

  • Reference data type Objectobject type
    • {} Ordinary objects
    • [] An array of objects
    • RegExp Regular objects
    • Function Function Data Types
    • Math Mathematical function object
    • Date Date Object

Data type scenarios:

When we pass Ajaxbefore the request to get the data, first in the local initialization data, typically will give a default data, then the requested data to the data into a data structure which we defined, for example:

var data = {

    count: 0,
    isActive: true,
    goods: [],
    model: null,
    message: ''
  
}

Verification data type typeofoperator

typeofOperator is used to detect a given type of data variables. typeofData types can be detected operators are:

  • undefined
  • boolean
  • string
  • number
  • object
  • function
var message = 'some string'
console.log(typeof(message) // "string"

var arr = [1,2,3,4]
console.log(typeof(arr) // "object"
console.log(typeof(null) // "object"

In the example above, the call typeof nullwill return "object", because of the special value nullis considered to be an empty object reference. By typeofdetecting whether a variable is an array is not accurate, it will return"object"

Guess you like

Origin www.cnblogs.com/dobeco/p/11618487.html