let obj={ a : 1, b : { c:'hello', d:true } }; console.log(obj.b.c); //输出:hello
let arr=[1,2,3]; console.log(arr.length); //输出:3 console.log(arr.map(v=>v*2)); //输出:[2,4,6]
// var定义的变量可以重复定义 var x=0; var x=1; // let定义的变量只允许定义一次 let x=0; let x=1;// 报错:SyntaxError: Identifier 'x' has already been declared // const定义的变量不可重新赋值 const x=1; x=2;// 报错:TypeError: Assignment to constant variable. //不允许换行,使用‘+’拼接字符串 var user='jishengming'; var x='hello-'+user; console.log(x);// 输出:hello-jishengming
const b='1'; if(b==1){ console.log('hello'); }// 输出:hello switch(b){ case '1': console.log('1的情况'); break; }// 输出:1的情况 console.log(b && 2);// 输出:2 console.log(b || 2);// 输出:1
for(let i=0;i<10;i++) { console.log(i); } let i=0; while(i<10){ console.log(i); i++; } let i=0; do{ console.log(i); i++; }while(i<10)
function hello(){ console.log('hello'); } hello();// 输出:hello function hello(name){ console.log(`hello ${name}`);// 注意符号,不是单引号 } hello('Ji Shengming');// 输出:hello Ji Shengming function hello(name = 'Ji Shengming'){ console.log(`hello ${name}`);// 注意符号,不是单引号 } hello();// 输出:hello Ji Shengming
// callback 异步回调 setTimeout(function(){ console.log('world'); },1000); console.log('hello');// hello输出1000ms后输出world setTimeout(function(){ console.log(1); setTimeout(function(){ console.log(2); },1000); },1000);// 1000ms后输出1,再1000ms后输出2 //promise const promise1=new Promise(function(resolve,reject){// 调用函数,两个参数分别表示成功,失败 setTimeout(function(){resolve('foo')},300); }); promise1.then((value)=>{ console.log(value);// 输出:foo }); console.log(promise1);// 输出:Promise //先输出promise1,300ms后再输出'foo' //settimeout async function timeout(time){ return new Promise(function(resolve){ setTimeout(resolve,time); }); } await time(1000); console.log(1); await time(1000); console.log(2);
const http=require('http'); http.createServer((req,res)=>{ res.writeHead(200,{'Content-Type':'text/html'}); res.write('hello world\n'); res.end(); }).listen(1337); console.log('服务器运行中。。。'); //打开网址(http://localhost:1337/)即可看到输出的内容:hello world
-内置模块:编译进 Node 中,例如 http fs net process path 等
-⽂件模块:原⽣模块之外的模块,和⽂件(夹)⼀⼀对应