Front-end learning JS (JavaScript) basic grammar

A basic introduction .JavaScript

A complete JavaScript essentially consists of three parts:
The first part: ECMAScript is the core of
the second part: Document Object Model (DOM) is mainly used to integrate js, css, html
Part II: Browser Object Model (BOM) is mainly used to integrate js and browser

Briefly JavaScript is a lightweight programming language, but he can be inserted into HTML, and then executed by the browser.

2.JavaScript two reference

//第一种:直接在Script标签中引入 如:
<script>
		JS代码
</script>
第二种:在Script标签中引入JS文件 如:
<script src="JS博客整理.js"></script>

The basic syntax of 3.JavaScript

1. Comment

/*
 * 这里是注释
 * */ 

2. To statement with a semicolon (;) at the end (or not write, but to write the habit)
3. variable declaration

var 变量名 = 变量的值;

Note: The variable can not start with a number, but you can start with $ character, generally not used at the same time, JS is the dynamic type of variables, a variable can be stored in different types of values, languages ​​like C does not have to declare the variable type.

//let命令声明的变量只在局部的代码块中有效
如:for(let i = 0; i<num; i++){
}
该语句中的i只在for循环中有效
//const命令可以声明常量,一旦声明就不可以再改变
如: const PI =3.1415926535;

4.JavaScript data types

1. Value Type

var a =1	var b =3.14		var c =1234e2

2. Special attention NaN, the full name (Not a Number) indicates that this is not a number

//NaN的用法
parsent("1232")					//会返回1232
parsent("你真美")				//会返回NaN,因为你真美不是一个数

2. String (String)

var String1 = “你”;
var String2 = "好";
var String3 = String1 + String2;
console.log(String3);			//这里即可输出你好二字
//由上面的例子可以看出字符串可以用加号(+)进行连接

3. The common method string

method Explanation
.length Returns the length of the string
.trim() Remove the string blank
.trimLeft() (.trimRight()) Removal string left (right) Blank
.charAt() Returns N characters
.concat(value,…) String concatenation
.indexOf(substring,start) Location subsequence
.substring(from,to) The acquisition sequence index
.slice(start,end) slice
.toLowerCase() lower case
.toUpperCase() capital

Note: To distinguish the difference between substring and slice of

4. Boolean value

var a = true;
var b = false;

Note: the empty string, 0, null, udefined, NaN is false;

5. Detailed null and undefined

  • represents null value is empty, empty is typically a variable is null, or empty direct assignment (var a = null).
  • undefined means that when you declare a variable but not yet initialized, the variable default value is the undefined.

**

The 5.JavaScript objects (object)

I believe my fellow learned object-oriented programming language, is no stranger to the definition of the object, here a brief introduction to some of the knowledge in JavaScript.
1. Array

定义:var a = {123 , "ABC"}
输出:console.log(a[1]);   输出ABC

2. The array of commonly used method

method Explanation
.length Length of the array
.push (he) Additional elements of the tail
.pop() Gets the element tail
.unshift(ele) Head insert elements
.shift Getting element of the head
.reverse() Reverse
.join(seq) The array element is connected to a string
.concat(val,…) Connection array
.sort() Sequence
.forEach() Each element of the array to the callback function
/*forEach的用法*/
array.forEach(function(v){				遍历数组元素
	console.log(v);
})

/*splice()的用法*/
splice(index,howmany,item1,...,itemX)		删除数组中的元素
其中index规定从哪里开始删除元素,howmany规定删除多少元素,item1为要添加的元素也可以不写

/*map()的用法*/
array.map(function(v){				返回该数组经过函数处理后的新数组
	处理该数组的语句
})

3. Type the query

用法:	   typeof	 "abc"				返回string
		   typeof	null				返回object
		   typeof 	true				返回boolean
		   typeof	123					返回number

Note: typeof is a unary operator, like addition, subtraction, not a statement is not a function

4. Operators

Operators and other languages ​​similar to the description herein only = difference == === No.

1.=是赋值号		如 a = 1;
2.==是若等于		 如 1 == "1"  		会返回true
3.===是强等于	 如 1 === "1"		会返回false

5. Process control

Process control, including if, if else, switch, for, the ternary operator, usage and C language usage when we first learned of similar.

6. function definition

/*函数有三种定义方式*/

1.普通函数的定义
function f1() {
	console.log("Hello World");
}
2.带参数函数的定义
function f1(a,b){
	console.log(a,b)
}
3.带返回值的函数
function sum(a,b){
	retrun a+b;
}
sum(1,2);
4.匿名函数的方式
var sum = function(a,b){
	return a+b;
}
sum(1,2);
5.立即执行函数的方式
(function(a,b){
		return a+b;
})(1,2);

7. Global and local variables
Local variables:
the JS variables declared inside a function (var) is a local variable, so it can only be accessed within the function. When the function is finished, the variable will be deleted.
Global variables:
variable declared outside a function are global variables, all functions on the page can access it.
8. lexical analysis (temporarily omitted, still do not understand)
**

6.JavaScript some built-in objects

NOTE: The difference var s1 = "abc" and var s2 = new string ( "abc") is that the former is returned string objects, which returns the object

1. Object types are described in

Types of Built-in objects Introduction
type of data Number Digital Object
String String object
Boolean Boolean value object
Grouping objects Array An array of objects
Math Mathematical objects
Date Date Object
Advanced Object Object Custom object
Error Error object
Function Function object
RegExp Regular expression object
Global Global Objects

2. The custom object
on JavaScript object (Object) is essentially a set of key-value pairs, but only as a key string.

var a = {"name":"wunan","age":20};
console.log(a.age);
console.log(a["age"]);
二者返回值都为20

To traverse all the contents of the object:

var a = (var i in a){
	console.log(i,a[i]);
}

Create Object

var person = new object();   	//创建一个person对象
person.name = "wunan";      	//给person对象中的name属性赋值wunan
person.age = 20;				//给person对象中的age属性赋值20

JSON object

var str1 = '{"name":"wunan","age":20}';
var obj1 = {"name":"wunan","age":20};
//将JSON字符串转换成对象
var obj = JSON.parse(str1);
//将对象转换成JSON字符串
var str = JSON.stringify(obj1);

Guess you like

Origin blog.csdn.net/w819104246/article/details/89433713