网页设计与网站建设期末考试,wordpress如何让别人注册,asp网站后台上传不了图片,那个网站做视频能挣钱文章目录 1. 构造函数1. 实例成员、静态成员2. 内置的构造函数 1. 构造函数
构造函数是一种特殊的函数#xff0c;主要用来创建对象。通过构造函数可以快速创建多个类似的对象 创建对象的三种方式 const obj1 {username:liuze};
// 方法1
const obj2 new Object();
obj2.u… 文章目录 1. 构造函数1. 实例成员、静态成员2. 内置的构造函数 1. 构造函数
构造函数是一种特殊的函数主要用来创建对象。通过构造函数可以快速创建多个类似的对象 创建对象的三种方式 const obj1 {username:liuze};
// 方法1
const obj2 new Object();
obj2.username liuze;
// 方法2
function Obj3(username){this.username username;
}
console.log(new Obj3(liuze));
// 方法3通过构造函数来创建对象构造函数大写字母开头的函数
1. 实例成员、静态成员
实例成员通过构造函数创建的对象称为实例对象实例对象中的属性和方法称为实例成员(实例属性和实例方法)
function Person(username,age){this.username username;this.age age;this.play function(){console.log(玩);}// 实例成员
}
const a new Person(liuze,23);
a.play();静态成员构造函数的属性和方法被称为静态成员静态属性和静态方法
function Person(username,age){}
Person.address 湖南
Person.play function(){console.log(玩);
}
// 静态成员2. 内置的构造函数
Object、Array、String、Number
JavaScript中最主要的数据类型有6种分别为5种基本数据类型和1种引用类型
5种基本数据类型为字符串、布尔、数值、undefined、null
1种引用类型为对象
其中字符串、数值、布尔等基本数据类型也都有专门的构造函数这些称为包装类型js种几乎所有的数据都可以基于构造函数创建。 Object内置的构造函数用于创建普通对象 静态方法Object.keys() 获取对象中的所有属性键 const obj {username:liuze,age:23};
console.log(Object.keys(obj));
// [username, age]Object.values() 获取对象中的属性值 console.log(Object.values(obj));
// [liuze, 23]Object.assign() 拷贝对象 const obj {username:liuze,age:23};
const obj2 Object.assign(obj);
console.log(obj2);
// {username: liuze, age: 23}Arrays 内置的构造函数用于创建数组 常见的实例方法 forEach遍历数组 filter过滤数组返回新数组 const arr [1,2,3,4,5,6,7,8];
const arr2 arr.filter(function(ele,index){// console.log(ele,index);return % 2 0;
})
console.log(arr2);
// [2, 4, 6, 8]map,迭代数组返回新数组 const arr [1,2,3,4,5,6,7,8];
const arr2 arr.map(function(ele,index){// console.log(ele,index);return ele * 2;
})
console.log(arr2);
// [2, 4, 6, 8, 10, 12, 14, 16]reduce累计器返回累计处理的结果用于求和 arr.reduce(function(上一次的值,当前值){},初始值) const arr [1,2,3,4,5,6,7,8];
const total arr.reduce(function(prev,curr){return prev curr;
});
console.log(total);
// 36
const total2 arr.reduce(function(prev,curr){return prev curr;
},10)
console.log(total2);
// 46join,对数组元素拼接为字符串返回字符串 find查找元素返回符合的第一个数组元素的值,如果没有返回undefined const arr [1,2,3,4,5,6,7,8];
const res arr.find(function(ele,index){return ele 9;
})
console.log(res);
// undefinedevery判断数组中每一个元素是否全都符合要求全部都满足要求返回true否则false const arr [1,2,3,4,5,6,7,8];
const res arr.every(function(ele,index){return ele 0;
})
console.log(res);
// falseString substring ,截取字符串 substring(indexStart[,indexEnd]),indexStart需要截取的首字母下标indexEnd需要截取的字符串的末尾字母下标(不包含) str 123
str.substring(0,1)
// 1startsWith检测某个字符串是否以另外某个字符串开头 startsWith(searchString[,position]),position表示从某个字符串哪个下标开始检测默认position0 const str1 12345;
console.log(str1.startsWith(123));
// true
console.log(str1.startsWith(123,0));
// true
console.log(str1.startsWith(123,1));
// falseincludes,检测某个字符串中是否存在另外一个字符串 includes(searchString[,position]) const str1 12345;
console.log(str1.includes(123));
// true
console.log(str1.includes(123,0));
// true
console.log(str1.includes(123,1));
// false