原先运行环境是网页,现在node能够模仿,所以本地运行需要下载node。
具体(在VScode中编辑):
1新建html文件,!+Tab获得:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <h1 >Hello World!</h1> <script src="文件名.js"></script> </body> </html>
|
处需要手动编辑:
1)h1+Tab获得标签,中间可编辑;
2)script+Tab获得标签,中间可编辑,但正常使用引用.js文件方式编辑,具体如上述代码。
2 javascript语法粗略学习
- 打印函数
console.log('hello world');
|
-
变量:var全局变量,let可修改,const不可修改赋值对象
-
原生数据类型:string,number包括小数,boolean,null,undefined
- undefined定义方法不是const而是:
- 类型检验方式:
此时null输出object
- string
1、打印方法(新/旧)
console.log('123'+变量名); //模板字符串 新方法: const hello = `Myname is ${变量名}`; console.log(hello);
|
2、内置方法
//变量名.length //.toUpperCase()/.toLowerCase大小写 //.substring(a,b)截取[a,b);并且可以后面叠加(如大小写) //.split('')分割成数组 最小以字母为单位 ‘’内传入分割依据
|
- 数组
//1)构造方法 //const 变量名 = new Array(1,2,3,4); //const 变量名 = [1,'1',true]; //2)索引 //数组名[1]可直接获取/赋值 //数组名.unshift('dhkaju')在最后加入数组 //数组名.pop()删除最后 //判断是否为数组Array.isArray(数组名)为bool值 //数组名.indexOf('数组内容')为数组内容索引号
|
- 对象
//const 对象名 = {} //属性可以TAB明确级别 //调用 // 对象名.属性名1,属性名2 // 对象名.属性名1,属性名1.1 // 对象名.属性名1[1] //剩余先跳过未学习
|
- if
if (条件句){ 内容 }else{ 内容 } //条件句:===同时判断类型是否相同,==只判断内容 // ||或 &&且
|
- 三目运算符
const color = x > 10 ? 'red' : 'blue'; //x > 10时color = 'red'反之'blue'
|
- swith条件语句
switch(color){ case 'red': break; default 'blue': break; }
|
- for while循环
for (let i = 0;i <= 10; i++){ 内容 } // let i = 0 while(i < 10){ 内容 i++; }
|