javascript
基础
//外链js脚本 支持相对路径 绝对网址
//<script scr='路径'></script>
/*
多行注释
Ctrl+/ 快捷键进行单行注释
Ctrl+Shift+/ 多行注释快捷键
*/
//"user strict"; 严格模式,写在js代码顶部
/*---------------------------------------变量------------------------------------*/
let message; //let声明一个变量
message = "给一个变量赋值"; //给一个变量赋值
message = "world"; //改变一个变量值
alert(message); //访问变量
let message = 'Hello!'; // 定义变量,并且赋值,写在一行
let user = 'John', age = 33, message = 'Hello~'; //一次性声明多个变量
let hello = "Hello world!";
let message = hello; //拷贝一个变量
//变量名可以是大小写字母,$,_,数字。 但数字不能在前
const myBirthday = '1988.08.03'; //声明一个常量,常量的值不能改
/*-------------------------------------数据类型-----------------------------------*/
//·1· Number类型
let n = 123.456; //可以是整型,也可以是浮点型。
let n = (1 / 0); //Infinity 代表无穷大
let n = ('字符串' / 2); //NaN 代表计算错误
let n = NaN ** 0; //1 唯一例外
//·2· BigInt类型
const bigInt = 1234567890123456789012345678901234567890n; //太大或太小的数, 结尾n表示是BigInt型
//·3· String类型
let str = "双引号字符串";
let str2 = '单引号字符串';
let phrase = `反引号字符串${str},${1 + 1}`; // 反引号字符串允许用${…}表达式嵌入变量或表达式!!!
//·4· Boolean类型
let isTrue = 2 > 1; //true
let isFalse = 2 === 1; //false
//·5· null值
let age = null; //无,空,值未知
//·6· undefined值
let kind; //undefined 未赋值
//·7·8· Object类型和Symbol类型
//object 伟大的对象
typeof 0 //“number” typeof用于查看数据类型
typeof null //“object” js的BUG
typeof alert //“function” 函数也属于object,但是typeof会区分对待函数,因为函数很重要~?
/*-------------------------------------交互函数-----------------------------------*/
alert("烦人的alert消息弹窗");
result = prompt('参数一提问内容', '参数二默认值(可选)'); //结果return给result,如果取消了返回null
result = confirm('接收一个问题参数,点确定返回true,点取消返回false')
/*-------------------------------------类型转换-----------------------------------*/
//字符串转换
let value = String(true); //"true"
//数字转换
let num = Number('123'); //123
let age = Number('无法转换的字符串会转换成NaN') //NaN 计算错误的数字类型
Number(undefined); //NaN
Number(null); //0
Number(true); //1
Number(false); //0
//布尔型转换
Boolean(); //0,空字符串,null,undefined,NaN 都转为false ;其他为true
/*-------------------------------------运算符-----------------------------------*/
// + - * / % **
(8 % 3); //2 %取余运算
(2 ** 4); //16 ** 求幂运算
(4 ** (1 / 2)); //2 求平方根 *两次0.5
(8 ** (1 / 3)); //2 求立方根 *3次0.5
let s = "my" + "string"; //mystring 拼接字符串
(2 + '1'); //"21" 有字符串就会自动转为字符串
(2 + 2 + '1'); //"41" 先计算2+2 当碰到字符串时再转字符串拼接
('1' + 2 + 2); //122 开始就是字符串,所以后面的自动转为字符串,不会做数学计算
(+""); //0 +会把其他类型强制转换成数字类型,等同于Number()
// 计算优先级排列 一元+ 一元- ** * / + — = 赋值=是最低级的,也就是说赋值运算是最后运行,所以赋给变量是最后的事
n += 5;//+= *= -= /= 只是简写形式
let counter = 2;
counter++ //3 ++自增1 --自减1 自增/自减只对变量有效,直接用于数字会报错
let counter = 1;
let a = ++counter; //a=2 ++放在前返回值是2
let counter = 1;
let b = counter++; //b=1 ++放在后面返回值还是1
//与& 或| 异或^ 非~ 左移<< 右移>> 无符号右移>>>
/*-------------------------------------比较-----------------------------------*/
//大于> 小于< 大于等于>= 小于等于<= 相对相等== 不等!= 绝对相等===
(null === undefined); //false
(null == undefined); //true
/*-------------------------------------if和?-----------------------------------*/
if (true) {
alert("do something")
} //true为任何表达式,结果为true便执行后面的代码 else;else if 可选
//?前面是条件 ?后面是表达式 :结束一个完整判定执行代码块
let message = (age < 3) ? 'Hi, baby!' : //if age<3
(age < 18) ? 'Hello!' : //else if age<18
(age < 100) ? 'Greetings!' : //else if age<100
'What an unusual age!'; //else
/*-------------------------------------逻辑运算符-----------------------------------*/
if (1 || 0) {
}
; // ||或 python的or
alert(undefined || null || 0); //可以用来寻找一个真值
true || alert("not printed"); //短路求值,因为第一个是true,所以不再执行alert
false || alert("printed"); //因为第一个是false,继续寻找真值,所以执行alert
if (hour === 12 && minute === 30) {
} // &&与 python的and
alert(1 && 2 && null && 3); //与运算与或相反,可以寻找第一个假值,如果所有的值都为真,就返回最后一个真
alert(!true); //false 非! 取反值
alert(!!null); //用!! 先取反布尔值,再取反布尔值,变相的转换了类型,取到真布尔值,等同于 boolean()
// result = a ?? b ??判断第一个值是否是已定义的值(不是null也不是undefined),那就返回真值
result = (a !== null && a !== undefined) ? a : b;
/*-------------------------------------while和for-----------------------------------*/
let i = 0;
while (i < 3) {
alert(i);
i++;
}
do { //do while 至少会运行一次循环体
alert(i);
i++;
} while (i < 3)
for (let i = 1; i < 3; i++) {
alert(i)
}
for (let i = 0; i < 10; i++) {
if (i % 2 == 0) continue; //如果为真,跳过循环体的剩余部分。 或者break直接跳出循环
alert(i); // 1,然后 3,5,7,9
}
/*-------------------------------------switch-----------------------------------*/
let a = 2 + 2; //如果没有break,就会执行严格相等后的所有代码
switch (a) {
case 3:
alert('Too small');
break;
case 4:
alert('Exactly!');
break;
case 5:
alert('Too big');
break;
default:
alert("I don't know such values");
}
/*-------------------------------------函数-----------------------------------*/
function f(a = 1, b = 1, c = 1) {
alert(a + b)
return b, c
}
let f = function (a = 2, b = 2, c = 2) { //函数表达式,先定义一个变量,然后把个匿名函数赋给这个变量
alert(a + b)
return b, c
}
//回调函数,f2,f3。。在调用f1的时候当作参数传入,按需调用~
function f1(q,f2,f3) {
if (q === "ok") f2()
else f3()
}
function f2() {
alert('是就回调我')
}
function f3() {
alert('不是就回调我')
}
f1("ok",f2,f3)
//箭头函数 函数表达式的变异体
let sum = (a, b) => a + b; //(参数) => {函数体}