functionadd(a =0, b =0){return a + b;}
console.log(add(1));// 输出1
console.log(add(1,2));// 输出3
arguments
数组元素个数
functionsum(){let result =0;for(let i =0; i < arguments.length; i++){
result += arguments[i];}return result;}
console.log(sum(1,2,3,4));// 输出10
console.log(sum(1,2,3,4,5));// 输出15
let numbers =[3,5,2,9,6];// 定义一个确定过滤规则的函数functiontest(value, index, array){return value %3===0;// 返回布尔值}
console.log(numbers.toString());// 输出3,5,2,9,6
console.log(numbers.filter(test).toString());// 输出3,9,6
console.log(numbers.toString());// 输出3,5,2,9,6
every方法
测试数组中全部元素,返回布尔值
let numbers =[3,5,2,9,6];// 定义一个确定测试规则的函数functiontest(value, index, array){return value >5;}let result = numbers.every(test);
console.log(result);// 输出false
some方法
测试数组中某些元素,返回布尔值
let numbers =[3,5,2,9,6];// 定义一个确定测试规则的函数functiontest(value, index, array){return value >5;}let result = numbers.some(test);
console.log(result);// 输出true
find方法
返回符合测试函数的第一个数组元素的值
let numbers =[3,5,2,9,6];// 定义一个确定测试规则的函数functiontest(value, index, array){return value >4;}let result = numbers.find(test);
console.log(result);// 输出5
findIndex方法
返回符合测试函数的第一个数组元素的索引
let numbers =[3,5,2,9,6];// 定义一个确定测试规则的函数functiontest(value, index, array){return value >4;}let result = numbers.findIndex(test);
console.log(result);// 输出1
map方法
通过对每个数组元素执行函数后返回新数组
方法不会更改原始数组
let numbers =[3,5,2,9,6];// 定义一个确定map规则的函数functionsquare(value, index, array){return value * value;}
console.log(numbers.toString());// 输出3,5,2,9,6
console.log(numbers.map(square).toString());// 输出9,25,4,81,36
console.log(numbers.toString());// 输出3,5,2,9,6
reduce方法
通过对每个数组元素从左到右依次执行函数
let numbers =[3,5,2,9,6];// 定义一个确定reduce规则的函数functionsub(total, value, index, array){return total - value;}let result = numbers.reduce(sub);
console.log(result);// 输出-19
reduceright方法
通过对每个数组元素从右到左依次执行函数
let numbers =[3,5,2,9,6];// 定义一个确定reduce规则的函数functionsub(total, value, index, array){return total - value;}let result = numbers.reduceRight(sub);
console.log(result);// 输出-13
数组排序
为sort方法提供一个比较函数实现排序
let numbers =[3,5,2,9,6];
numbers.sort(function(a, b){return b - a;// a - b为升序,b - a 为降序});// 类比java的Comparable接口
console.log(numbers.toString());// 输出9,6,5,3,2
let team =[{sname:'Alice',age:19},{sname:'Bob',age:18},{sname:'Tom',age:20},];
多维数组
数组的元素还是数组
let table =[];for(let row =0; row <9; row++){
table[row]=newArray(row);for(let col =0; col <= row; col++){
table[row][col]=(row +1)*(col +1);}}for(let i =0; i < table.length; i++){
console.log(table[i].toString());}
对象
对象的创建与使用
let student ={sid:'1083710314',sname:'Alice',sayHello:function(){
console.log("Hi, I am "+this.sname +"!");},courses:{c1:'C',c2:'Java',c3:'JS'}//也是对象}
let student ={sid:'1083710314',sname:'Alice',sayHello:function(){
console.log("Hi, I am "+this.sname +"!");},//student是具体的对象courses:{c1:'C',c2:'Java',c3:'JS'}//冒号左边是变量名,冒号右面可以是任意东西}// 输出Alice
console.log(student.sname);// 输出Hi, I am Alice!
student.sayHello();// 为student对象增加属性major
student.major ='CS';// 输出CS
console.log(student.major);// 输出 {c1: 'C', c2: 'Java', c3: 'JS'}
console.log(student.courses);
delete删除对象属性,in判断属性是否存在
let student ={sid:'1083710314',sname:'Alice',age:18}
console.log('age'in student);// 输出truedelete student.age;
console.log('age'in student);// 输出false
对象构造器
可以构造类型相同的多个对象
jS没有类也可以构造对象
functionStudent(sid, sname){this.sid = sid;this.sname = sname;this.sayHello=function(){
console.log("Hi, I am "+this.sname +"!");}//这个Student是类型,类型是function}let alice =newStudent('1083710314','Alice');let bob =newStudent('1083710315','Bob');
// 输出Hi, I am Alice!
alice.sayHello();// 输出Hi, I am Bob!
bob.sayHello();// 为alice对象增加属性major
alice.major ='CS';// 输出CS
console.log(alice.major);// 输出undefined
console.log(bob.major);//bob没有专业
对象原型
使用对象的prototype属性为对象构造器增加属性
functionStudent(sid, sname){this.sid = sid;this.sname = sname;}let alice =newStudent('1083710314','Alice');let bob =newStudent('1083710315','Bob');Student.prototype.sayHello=function(){// 为对象原型增加属性
console.log("Hi, I am "+this.sname +"!")}
alice.sayHello();// 输出Hi, I am Alice!
bob.sayHello();// 输出Hi, I am Bob!
let alice =newStudent('1083710314','Alice',1990);// 可以通过原型动态为类增加方法Student.prototype.sayHello=function(){
console.log("Hi, I am "+this.sname +"!");}
console.log(alice.getAge());// 输出31
alice.sayHello();// 输出Hi, I am Alice!
import{userid, password}from'./common/profile.js';if(userid ==='admin'&& password ==='123456'){
console.log('Welcome!');}else{
console.log('Access is denied!');}//./index.js
node.js使用import需定义package.json
{"type":"module"}//./package.json
一次导出多个变量
导入时可以重名
let userid ='admin';let password ='123456';export{ userid, password };//./common/profile.js
import{userid as uid, password as pwd}from'./common/profile.js';if(uid ==='admin'&& pwd ==='123456'){
console.log('Welcome!');}else{
console.log('Access is denied!');}
导入整个模块
使用星号
将模块重命名
let userid ='admin';let password ='123456';export{ userid, password };//./common/profile.js
import*as user from'./common/profile.js';if(user.userid ==='admin'&& user.password ==='123456'){
console.log('Welcome!');}else{
console.log('Access is denied!');}
函数导出与导入
functionadd(a, b){return a + b;}functionsub(a, b){return a - b;}export{ add, sub };
import{add, sub}from'./common/math.js';let x =5;let y =8;
console.log(add(x, y));// 输出13
console.log(sub(x, y));// 输出-3
对象导出与导入
exportlet user ={"data":{"userid":"admin","password":"123456"}}
import{Student}from'./common/hit.js';let alice =newStudent('1083710314','Alice',1990);Student.prototype.sayHello=function(){
console.log("Hi, I am "+this.sname +".");}
console.log(alice.getAge());// 输出年龄
alice.sayHello();// 输出Hi, I am Alice.
默认导出
文件
同步获得文件信息
statSync
let fs =require("fs");let stats = fs.statSync('./source.txt');
console.log(stats.size);