目录
// 🏆 构造函数 :是一种特殊的函数,主要用来初始化对象
//为了区分普通函数
// 1.构造函数函数名首字母需要大写
// 2.使用new关键字调用函数
//构造函数的作用:主要是为了创建对象
🏆构造函数里面的this代表了实例化对象
// 🏆 实例化执行过程
// 说明:
// 1. 创建新对象
// 2. 构造函数this指向新对象
// 3. 执行构造函数代码,修改this,添加新的属性
// 4. 返回新对象
// 🏆new关键字主要做了以下的工作:
// 创建一个新的对象obj
// 将对象与构建函数通过原型链连接起来
// 将构建函数中的this绑定到新建的对象obj上
// 根据构建函数返回类型作判断,如果是原始值则被忽略,如果是返回对象,需要正常处理
// 成员:对象中的属性和方法
// 🏆实例成员:
// 通过构造函数创建的对象称为实例对象,实例对象中的属性和方法称为实例成员。
// 说明:
// 1. 实例对象的属性和方法即为实例成员
// 2. 为构造函数传入参数,动态创建结构相同但值不同的对象
// 3. 构造函数创建的实例对象彼此独立互不影响。
- function Person() {
- this.uname = 'zs'
- this.sayHello = function () {
- console.log(1)
- }
- }
- const p1 = new Person()
- console.log(p1.uname)
- p1.sayHello()
// 🏆静态成员:
// 在构造函数的属性和方法被称为静态成员
// 静态成员只能被 构造函数访问
// 说明:
// 1. 构造函数的属性和方法被称为静态成员
// 2. 一般公共特征的属性或方法静态成员设置为静态成员
// 3. 💎静态成员方法中的 this 指向构造函数本身
- Person.aname = '王五'
- console.log(Person.aname);
-
- Person.hello = function () {
- console.log(this === Person);//ture
- }
- Person.hello()
// 🏆其实字符串、数值、布尔、等基本类型也都有专门的构造函数,这些我们称为包装类型。
//JS中几乎所有的数据都可以基于构成函数创建。
//系统几乎为每一数据类型都提供对应构造函数
/*
数据类型
内置构造函数
number Number()
String() string
boolean Boolean()
无 undefined
无 null
Array() array
object() object
function Function()
*/
//只要是函数就可以使用new来调用
new Number();
new String();
new Boolean();
// 小结:
// 系统内置的构造有哪些?除undefined与null外都有一种对应的构造函数()
// 只要是构造函数就是使用new来调用,从而得到对象
// 不构造函数得到的是不同类型的对象
//任何一个数据都是其对象的构造函数的实例
//首先明确一点
//.运算符是用于对对象的成员进行操作
let obj = {};
//通过typeof查看数据是 'object'
console.log(typeof obj);
//即然是对象就可以使用.进行成员操作
obj.age = 20
let i = 10;
//通过typeof查看10是 'number'
console.log(typeof i);
//数据10它的类型并不是object,尝试在i后面进行.操作
i.age = 20
//i里的数据不是对象,但进行.操作,并没有报错,原因是js内部进行处理
//🏆基本包装数据类型
//在Js进行,操作时,如果检测到.前面的数据不是对象,会将使用这种数据对象对应的构造函数,临时构造出来一个对象,并在这个临时构造出来的对象上完成本次的.操作。当本次点操作结束后,会将这个临时对象销毁
// 🏆 能够进行基本包装的数据类型有:
// number、string、 boolean
// Object是系统提供的构造函数
// {
// key:value,
// key:value,
// }
/*💎静态方法
Object.keys(对象) 获取一个对象上所有的成员名
Object.values(对象) 获取一个对象上所有的成员值
Object.assign(目标对象,源对象,源对象,...) 用于复制对象的成员
将源对象上的成员,复制给目标对象,用于复制对象的成员
*/
- let obj = {
- uname: '王建华',
- age: 40,
- gender: '男'
- }
- let arrkeys = Object.keys(obj)
- console.log(arrkeys);
-
- const arr=Object.values(obj)
- console.log(arr);
-
- let a = {
- tel: '13798908',
- email: 'asdhjahd@asd.com'
- }
- Object.assign(obj, a)
- console.log(obj);
- a.tel='1578900'//修改a对象的内容,obj没有发生变化
- console.log(a);

//🏆数组.reduce()
// 用于对数组进行遍历,并进行统计
/*
//语法:
数组.reduce(function(){},初始值)
数组.reduce(function(pre,item,index){
},初始值)
说明:
reduce会重复调用function(pre,item,index){}
reduce在首次调用时会将 初始值 传递给 pre
reduce在其后各次调用时,会将从function(pre,item,index){}接收到的返回值传递给 pre
*/
- // 🏆reduce实现累加求和
- // 💎 有初始值时 reduce() 如何运行
- // let arr = [1, 3, 9, 15, 20];
- // let ret = arr.reduce(function (pre, item, index) {
- // return item + pre;//返回值会作为下一次的pre的值
- // }, 0)
- // console.log(ret);
- //💎 无初始值时 reduce() 如何运行
- //没有起始值
- //第一次循环的时候会将数组里面的第一个元素给temp变量item变量就是数组的第二个元素
- //如果没有起始值 reduce则从数组的第二个元素开始循环/将数组的第一个元素给 temp 变量将数组的第二个元素给 item
- // 如果有起始值 reduce则从数组的第一个元素开始循环将起始值给temp 变量将数组的第一个元素给 item变量
- //如果回调函数里面没有return则第二次以后temp为undefined
- //如果 回调函数里面 有 return则返回值作为下一次的temp变量的值
- const array = [15, 16, 17, 18, 19];
-
- function reducer(previous, current, index, array) {
- const returns = previous + current;
- console.log(`previous: ${previous}, current: ${current}, index: ${index}, returns: ${returns}`);
- return returns;
- }
-
- array.reduce(reducer);
- DOCTYPE html>
-
-
-
-
-
Document -
-
-
- 1
-
- 2
-
- 3
-
- 4
-
- 5
-
-
- //💎数组.every() //遍历数组,并判断是否每个元素都满足条件,如果全满足返回true,否则返回false
- //💎数组.some()//遍历数组,并判断是否有满足条件的元素,如果有返回true,否则返回false
- // const arr = [14, 342, 23412, 34, 18]
-
- // // 判断是否全是偶数
- // // let res = arr.every(item => item % 2 === 0)
- // // console.log(res);
- // // 判断是否有奇数
- // let res = arr.some(item => item % 2 !== 0)
- // console.log(res);
-
- // 💎伪数组转化为真数组Array.from()
- const lis = document.querySelectorAll('li')//伪数组
-
- let liarr = Array.from(lis)//转化为真数组
- // 真数组可以使用数组的方法
- liarr.pop()
- console.log(liarr);
-
-
- // 💎reverse 颠倒数组
- // arr.reverse()
- // console.log(arr);//直接修改数组,不用赋给一个变量
-
- // 💎findIndex 查找索引值,return后加条件
- // const index = arr.findIndex(item => item === 34)
- // console.log(index);
-
-
- // const person = [
-
- // { id: 1, name: 'zs', age: 20 },
- // { id: 2, name: 'zs', age: 20 },
- // { id: 3, name: 'zs', age: 20 },
- // { id: 4, name: 'zs', age: 20 }
-
- // ]
- // const ix =person.findIndex(item => item.id === 4)
- // console.log(ix);
-
- // 💎concat方法
- const arr1 = [1]
- const arr2 = [2, 3]
- const arr3 = arr1.concat(arr2)
- console.log(arr3);
-
-
-
-
-
-
-
-
-
-
-
数组find方法
//🏆用于查找数组中满足条件的第1个元素,如果有则返回满足条件的第一个元素,如果没有这样的元素则返回undefined
/*
数组.find(function(item,index){
return 条件
})
- //查找数组>5的第1个元素
- // let arr = [5, 4, 7, 19, 20];
- // let ret = arr.find(function (item, index) {
- // // console.log(item,index);
- // return item > 5
- // })
- // console.log(ret);//7
-
-
- const arr = [
- { id: 5, name: 'lisi', age: 25 }
- , { id: 6, name: 'wang', age: 22 }
- , { id: 7, name: 'liu', age: 45 }
-
- ]
- // 获取name为liu的id和年龄
- const res = arr.find(item => item.name === 'liu')
- console.log(res);
- console.log(res.id, res.age);
- DOCTYPE html>
-
-
-
-
-
Document -
-
- //💎字符串.split(分割符)
- //将字符串使用指定的分割符进行分割,并分割的每个部分组织成数组返回。
- // let str = 'one,two,three';
- // let ret = str.split(',');
-
- // console.log(ret);
- // console.log(str.split(""));
-
-
- //字符串也可以当成由多个字符组成的序列
- //字符串也可以通过下标来读取某个位置上的元素
- // let str = 'hello'
- // console.log(str[0]);
-
- //💎字符串.substring(start,end)
- //在字符串中,从start指定的下标开始,读取到end所指定的下标之间的字符
- //含头不含尾
- //如果省略第二个参数,表示读取到最后
- // let url = 'index.html';
- // let ret = url.substring(5, 5 + 5)
- // console.log(ret)
-
- //💎字符串.startswith(子字符串)
- // str.startsWith(字符串[, 下标])//下标不写则默认为0
-
- // 判断字符串是否以子字符串开头,如果是则返回true,否则返回false
- // let url = 'http://www.xxx.com/xxx'
- // let ret = url.startsWith('http://')
- // console.log(ret);
- //字符串.endswith(子字符)
- //判断字符串是否以子字符串结尾,如果是则返回true,否则返回false
-
-
- //💎字符串.includes(子字符串)
- //判断字符串中,是否包含子字符串,如果包含返回true,否则返回false
- // let url = 'http://www.xxx.com/xxx'
- // let ret = url.includes('.cn')
- // console.log(ret);
-
-
- // 💎padStart() 方法用另一个字符串填充当前字符串 (如果需要的话,会重复多次),以便产生的字符串达到给定的长度。从当前字符串的左侧开始填充。
- // padStart(预期的字符串长度,填充的字符)
- // const str = '9'
- // const res = str.padStart(5, '#')//如果str长度不够2,则用0在str最前面填充,直到2
- // console.log(res)
-
- // // 可以代替之前的时分秒补零操作
- // const num = '9'
- // const ret = num.padStart(2, '0')
- // console.log(ret);
-
-
- // 💎toUpperCase() 方法将调用该方法的字符串转为大写形式并返回(如果调用该方法的值不是字符串类型会被强制转换)
- // const sentence = 'you are pig! ';
- // console.log(sentence.toUpperCase());
-
- // // 把str首字母变成大写字母
- // const str = 'checkUserName'
- // // 把首字母转为大写toUppercase(),然后和后面的字符拼接在一起subString()
- // const res = str[0].toUpperCase() + str.substring(1)
- // console.log(res);
-
-
- // 💎字符串.indexOf('查找的字符串') 查找字符串里面指定的字符找到了返回其下标找不到返回-1
- // arr.indexOf(searchElement[, fromIndex])查找元素,开始查找的下标
-
- str = 'JavaScript'
- console.log(str.indexOf('t',4));
- console.log(str.indexOf('b',4));
-
-
-
-
-
- //🏆toFixed用于保留指定位数的小数,进行四舍五入
- let price = 12.357
- let ret = price.toFixed(2)
- console.log(ret);//12.36
-
-
- //🏆tostring()将数组转换为字符串
- let n = 10;
- //将数组转换为字符串
- console.log(typeof String(n))
- console.log(typeof n.toString())