content_position
- inside
prompt : The best position is in front of thebody
- external
prompt : this sentence will be ignore if placed in the middle
<script src='[position]'></scripy>
- line
annotation
-
line
//
-
block
/* */
end_mark
;
Ther is no constraint, suggest uniformity throughout entire item
input-output
input
prompt()
output
-
window
slert()
-
write to body
document.write()
Html
will be explained as write -
console print
console.log()
variable
declare
let variable
- let assign value
- Isolate and declare several variable using a comma
,
var variable
This is obsolete , do not use it
-
It is irrational to declare variable before using it
-
It is irrational to declare variable before variable
-
variable hint , global variable , not block scope , a series of
const variable
recommend use
- second assign value
- Force the assignment of a value at the time of declaretion
law
- Do not use the keyword
- Variable names use must underline , letter , number , or the
$
symbol but must not begin with a number - Strict case sensitivity of letter
rule
-
name have meaning
-
name use Camel-case
Use lowercase for the first letter and uppercase for initial letters of subsequent words
基本数据类型
-
number数值型
包含所有数值 -
string字符串
- 单引号
- 双引号
- 反引号
字符串拼接${}
-
boolean布尔类型
- true
- flase
-
undefined未定义类型
只声明未定义
-
null空
官方解释将null作为尚未创建的对象 -
检测
typecho 变量
typecho (变量)
转换
隐式转换
规则:
-
+
号两边只要有一个是字符串,就会把另外一个也转为字符串 - 除一位的算数运算符,比如
- * /
等都会把数据转为数字类型
小技巧
- +号作为正号解析可以转为数字型
- 任何数据和字符串相加都是字符串
显示转换
数据类型(变量)
-
数字型
-
Number
转换失败为NaN
-
parseInt
只保留整数
-
parseFloat
只保留小数
-
运算符
优先级 | 运算符 | 顺序 |
---|---|---|
1 | 小括号 | () |
2 | 一元运算 | ++ — ! |
3 | 算数运算 | 先* / % 后 + - |
4 | 关系运算符 | > >= < <= |
5 | 相等运算符 | == != === !== |
6 | 逻辑运算符 | 先&& 后|| |
7 | 赋值运算符 | = |
8 | 逗号运算符 | , |
数组
声明
- let 变量 = []
- let 变量 = new Array()
操作
-
增
arr.push(新增的内容)
:将一个或多个元素添加到数组的末尾,并返回数组的新长度
arr.unshift(新增的内容)
:将一个或多个元素添加到数组的开头,并返回数组的新长度 -
删
arr.pop()
arr.shift()
arr.splice(start,deletecount)
start : 指定修改的起始位置(默认从0开始)
deletecount : 要移除的元素的个数,可选,如果省略默认从起始删到最后 -
改
数组[下标] = 新值
-
查
数组[下标]
语句
分支语句
-
if
if (){}else if(){}else{} -
三元运算
条件?true:false -
switch
switch(data){case value1:code1breakcase value2:code2breakdefault:codenbreak}
循环语句
-
while
while(循环条件){循环体} -
for
for(;;){循环体} -
退出循环
continue
:退出本次循环
break
:退出整个循环
函数
声明函数
function 函数名(){ 函数体}
- 形参创建不需要声明
- 推荐给形参赋一个初始值
传参
- 如果传入实参过多,会被忽略;传入过少,没接收到的形参为undefined
返回
return 数据
作用域
如果在函数内使用变量,没有声明直接使用,该变量会变成全局变量
匿名函数
let 变量=function (){ 函数体}
具名函数可以随意位置调用
匿名函数只能声明后调用
立即执行
-
先创建函数,再调用
(function(){})();第一个小括号为创建函数,第二个小括号为调用函数
-
创建好函数,直接调用
(function(){}())里面第一个小括号为函数参数,里面第二个小括号为调用函数
必须要加
;
,也能加在前面
逻辑运算符里的短路
只存在&&
或||
中
符号 | 短路条件 |
---|---|
&& | 左边为false就短路 |
|| | 左边为true就短路 |
Object
declare Object
let name = {}
let name = new Object()
- let assign value/function
- anonymity function if use
opration
-
Find/Use value
object.attribute
object['attribute']
-
Add/Alter value/function
object.attribute=value/function
-
Use function
object.attribute.function(value1,value2)
iterate
for (let i in object){ i //'key' object[i] //value}
Built-in math object
Math.random()
: random number [0,1)
Math.ceil()
: method for rounding up
Math.floor()
: method for rounding down
Math.max()
: find max number
Math.min()
: find min number
Math.pow()
: exponentiation
Math.abs()
: absolute value