地址:https://code.visualstudio.com/(microsoft开源,开箱即用)
地址:http://nodejs.cn/
安装前先install python(建议2.0和nodejs配套)
Node.js是可以直接再服务端运行js代码,基于chrome的V8引擎,可以脱离浏览器来执行js代码;性能比较好;
console.log("hello,this is my first js script")
node xxx.js
const http = require('http');
http.createServer(function(request, response){
response.writeHead(200, {'Content-Type': 'text/plain'})
response.end('hello,node server')
}).listen(8080)
console.log("Server running at http://localhost:8080")
NPM的全称是Node Package Manager,是一个NodeJS包管理和分发工具,已经成为了非官方的发布Node模块(包)的标准。类似于maven,所有东西只要导入依赖即可,npm也是如此:npm install;好比Linux的yum安装
初始化
初始化结果
使用命令
npm init
npm init -y //finish the initialization by default value
查看npm的配置信息
vpm config list
修改配置
npm config set registry “https://registry.npm.taobao.org”
command:
npm install jquery
the reusult of installing
package.json is where managed the version control for all dependencies,like pom.xml in maven
command
npm install [email protected]
npm install --save-dev eslint
npm install --save-D eslint //same as former
npm install -g webpack
npm update // update all dependencies
npm update jquery // update the specified package based on the first version x.1.1
//Or just modify the value of the version in the package.json
npm uninstall jquery // install the specified package
很多se6高级语法,浏览器是不支持的,Nodejs也不一定能运行!
转码器
Babel是一个广泛的转码器!可以将ES6代码转换未ES5的代码!语法会自动转换!
1.安装babel
npm install -g babel-cli
let input = [1, 2, 3]
input = input.map(item => item + 1)
console.log(input)
{
"presets": [
"es2015"
],
"plugins": []
}
npm install -D babel-perset-es2015
babel .\src\example.js --out-file .\dist1\dist.js
"use strict";
var input = [1, 2, 3];
input = input.map(function (item) {
return item + 1;
});
console.log(input);
在package.json中设置
npm run test
4.VSCode使用label命令报错解决
babel : 无法加载文件 C:\Users\yanghai\AppData\Roaming\npm\babel.ps1,因为在此系统上禁止运行脚本。
发展
网站一步一步变成互联网化,js代码越来越大,十分复杂!
js开始走向模块化
class,package,moudle
commonJS规范使用
1.定义commonJS并导出module
const sum = (a, b) => a + b
const sub = (a, b) => a - b
const mul = (a, b) => a * b
const div = (a, b) => a / b
//export the methods for the others
// module.exports = {
// sum:sum,
// sub:sub,
// mul:mul,
// div:div
// }
//abbreviate if there are the same name
module.exports = {
sum,
sub,
mul,
div
}
2.导入module使用
const cal = require('./caculate.js')
console.log(cal) /*{
sum: [Function: sum],
sub: [Function: sub],
mul: [Function: mul],
div: [Function: div]
}*/
const r1 = cal.sum(1, 99)
console.log(r1) //100
ES6模块化规范
1.单独导出导入
export function getList() {
console.log('get the user list')
}
export function saveList() {
console.log('save the user information')
}
//node doesn't support the import
import {
getList, saveList} from './userApi.js'
getList()
saveList()
ES6第二种写法
export default {
getList2(){
console.log('get the user list')
},
saveList2(){
console.log('save the user list')
}
}
import user from './userApi.js'
user.getList2()
user.saveList2()
注意: node doesn’t support the module of ES6 ,needs the label transferred meaning