配置环境
- 安装node 、vsCode
- vsCode安装 Live Server 插件:运行html文件到浏览器上,可通过js控制台查看输出
- vsCode安装 Run Code 插件:直接编译js文件,在下方控制台查看输出
变量与常量
字符串
const msgs = new String();
const msgs = 'Hello World WKW';
console.log(msgs);
msgs.length
msgs[0]
msgs.toUpperCase()
msgs.toLowerCase()
msgs.substring(0,5)
msgs.split(' ')
let m = msgs + "拼接";
msgs.indexOf("Hellow")
数组
const arr = new Array();
const arr1 = [];
const arr2 = ["11","22","33"];
console.log(arr2[1]);
arr2.push("44");
arr2.pop();
arr2.unshift("00");
console.log(Array.isArray(arr2));
console.log(arr2.indexOf("11"));
var a = [1,2,3,4,5,6,7,8,9,0];
var s = a.toString();
console.log(s);
var a = [1,2,3,4,5,6,7,8,9,0];
var b = \[1,2,3,4,5,6,7,8,9,0];
var s = a + "," + b;
console.log(s);
var a = \[1,\[2,3],\[4,5]],\[6,\[7,\[8,9],0]]];
var s = a.toString();
console.log(S);
对象/字典
//定义
const person = {
firstName: "First",
lastName: "Last",
age: 22,
hobbies: ["music","movies","football"],
address: {
street: "sssss",
city: "jinan",
state: "shandong"
}
};
//追加
person.email = "5555@163.com";
//转换成json串
const toJson = JSON.stringify(person);
console.log(toJson);
//输出{"firstName":"First","lastName":"Last","age":22,"hobbies":["music","movies","football"],"address":{"street":"sssss","city":"jinan","state":"shandong"},"email":"5555@163.com"}
if 条件语句
var x = 10
var y = 20
if (x == 10) {
console.log("true")
} else {
console.log("false")
}
if (x > y) {
console.log("true")
}
if (x == 10 && y == 20) {
console.log("true")
}
if (x == 10 || y == 20) {
console.log("true")
}
//三目运算
const color = x == 10 ? 'red' : 'blue'
switch 条件语句
switch (color) {
case "red":
console.log("color: red");
break;
case "blue":
console.log("color: blue");
break;
default:
console.log("color: other");
break;
}
循环
for (let i = 0
console.log(i)
}
let a = 10
while (a < 20) {
console.log(a)
a++
}
不看了,直接卷vue去