函数
/*
创建一个方法
形参不需要写参数类型,多个参数直接用逗号隔开就可以
*/
function show(a,b,c){
console.log('我是show方法',a,b,c)
return a+b;
}
// 调用方法
//调用方法时 形参个数如果和实参个数对不上不会有异常
show();
show('你好')
show(1,2);
let add = show(1,2,3);
console.log(add);
作用域提升
js和其他语言一样,都要经历编译和执行阶段。而js在编译阶段的时候,会搜集所有的变量声明并且提前声明变量,而其他的语句都不会改变他们的顺序,因此,在编译阶段的时候,第一步就已经执行了,而第二步则是在执行阶段执行到该语句的时候才执行。
对象
创建方式1 字面量创建
/*
字面量创建对象
let 对象名 = {属性名:属性值,属性名:属性值,方法名:函数};
*/
let student = {
name:'张三',
age:10,
className:'2312',
study:function(){
console.log('我在学习');
}
}
console.log(student)
//使用对象的属性和方法 通过对象.的形式
console.log(student.name)
student.study();
创建方式2 Object创建
/*
创建空对象赋值的方式
//利用内置对象Object创建对象:
var 对象名 = new Object();
对象名.属性名=属性值;
*/
let stu = new Object();
stu.name='张三'
stu.sex='男'
stu.study=function(){
console.log('我在学习')
}
console.log(stu)
创建对象方式3 自定义构造器创建
/*
通过自定义创建一个构造器,传入指定的参数
因为构造器长得和函数一样,一般首字母都是大写
通过此构造器创建多个对象
*/
//创建构造器
function Student(name,sex,age,className){
this.name=name,
this.age=age;
this.sex=sex;
this.className=className;
}
//通过构造器创建对象 new
let stu1 = new Student('张三2','男',20,'2312');
let stu2 = new Student('张三1','男',20,'2312');
console.log(stu1,stu2)
遍历当前对象的属性
//遍历对象属性名 通过属性名获取属性值
for (const key in stu1) {
//判断当前属性 是否是这个对象的属性
if (Object.hasOwnProperty.call(stu1, key)) {
const element = stu1[key];
console.log(key,element)
}
}
创建对象方式4 原型模式+构造函数
每一个函数都有一个prototype(原型)属性,这个属性是一个指针,
指向一个对象,而这个对象的用途是包含可以由特定类型的所有实例共享的属性和方法。
也就是说,prototype是通过调用构造函数而创建的那个对象实例的原型对象。
function Person(name,age,sex){
this.name=name;
this.age=age;
this.sex=sex;
}
//获取Person的原型对象
// Person.prototype.city='山西';
Person.prototype={
constructor:Person,
city:'山西',
run:function(){
}
}
let person1 = new Person('张三',20,'男');
console.log(person1)
创建对象方式5 通过class类模板创建
//通过class模板创建
class Student{
constructor(name,age,className){
this.name=name;
this.age=age;
this.className=className;
}
// 普通方法不需要添加function
study() {
console.log(this.name+'在学习')
}
}
//通过new关键字创建模板对象
let stu1 = new Student('张三',20,'2310');
stu1.study();
console.log(stu1);
注意:ES6版本之后 退出了class关键字 ;
//通过class模板创建
class Student{
constructor(name,age,className){
this.name=name;
this.age=age;
this.className=className;
}
// 普通方法不需要添加function
study() {
console.log(this.name+'在学习')
}
}
//通过new关键字创建模板对象
let stu1 = new Student('张三',20,'2310');
stu1.study();
console.log(stu1);
静态方法属性
class Person{
static realname = '手机';
static play(){
console.log("会玩游戏!");
}
}
const p1 = new Person();
console.log(p1.realname);//undefined
Person.play();//会玩游戏
正则验证
/*
正则 :正则表达式
主要用来验证用户上传或者输入的内容是否满足格式
长度验证,非空验证 .length ==null ==""
验证手机号是否满足格式:
长度11
1开头
身份证:
18位
*/
// 手机号的正则表达式 /^1[3456789]\d{9}$/
// let reg = /^1[3456789]\d{9}$/
let reg =/^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/
let boo = reg.test('1407021212127038');
console.log(boo)
Math数学类
max(值);min(值);random() // 随机数: 0~1;
返回指定范围的随机数(m-n之间)的公式
Math.random()*(m-n)+nround() // 四舍五入:
ceil() // 向上取整:
floor() // 向下取整
Date时间类
var date = new Date();//可以获取到当前时间:
console.log(date.toLocaleDateString());2023/4/28
var year = date1.getFullYear(); // 获取年:var month = date1.getMonth()+1; // 获取到月;
var date2 = (date1.getDate()>9)?date1.getDate():'0'+date1.getDate(); //获取
日: 如果日期>9显示日期否则显示0日期
var hour = date1.getHours();// 获取到小时;
var minute=date1.getMinutes(); // 获取到分;
var second= date1.getSeconds();//获取到秒;
var day = date1.getDay();//获取星期几
console.log(year+'-'+month+'-'+date2+' '+hour+':'+minute+':'+second+' '+day);
Array 数组工具
//排序
// arr.sort(function(o1,o2){
// // 0 -1 1
// return o1-o2;
// });
console.log(arr)
// 删除
// 第一个元素是从哪开始删 第二个是删除几个
// arr.splice(1,2);
//插入
// 如果第二个参数为0 第三个参数会充当索引上的元素
arr.splice(1,0,99);
console.log(arr)