bind的用法
示例
// bind 创建一个新的函数,并传入this
var name = "qqq";
var age = 18;
function say(a,b){
console.log(`大家好,我的名字是${this.name}今年${this.age}`)
console.log(a,b,a+b);
}
// say();
var nsay = say.bind({name:"秀儿",age:20},10);
// say通过bind创建一个新的函数 this为bind的第一个参数,a的值是10
nsay(7);
set
示例
//set 一个不重复数组
//对数组 去重
var arr = [1, 2, 1, 3, 2, 7, 8, 7];
var s1 = new Set(arr);
arr = [...s1];
arr = Array.from(s1);
console.log(arr);
set方法例
示例
// 初始化 new Set()
var s = new Set([1,3,1,3,5,7,9]);
console.log(s);
// 添加 add()
var s = new Set([1,3,1,3,5,7,9]);
s.add(12);
console.log(s);
// 删除 delete()
var s = new Set([1,3,1,3,5,7,9]);
s.delete.log(1);
console.log(s)
// 获取长度 size
var s = new Set([1,3,1,3,5,7,9]);
console.log("size:",s.size);
// 遍历 for of
var s = new Set([1,3,1,3,5,7,9]);
for(let i of s){
console.log(i);
}
map
// map 类似于对象
//键名可以是任意对象,对象:键名只能是 字符串 或者 symbol
// map是有序,对象:按默认排序
// 长度 size
// 添加 set
// 获取 get
// remove()
// 变量 for of
var obj = {"b":100,"a":"wwe","2":"best","1":"good"};
console.log(obj);
var map = new Map([["b",100],["a","wwe"],[2,"best"],[1,"good"]])
console.log(map);
symbol 符号
// 常用于左右对象的key
//初始化
s1 = Symbol("blue")
var obj = {[s1]:"www"}
Symbol.for("blue") == Symbol.for("blue") //True
Symbol("blue") == Symbol("blue") //False
for of
// string 字符串 Number 数字 Boolean布尔 undefined未定义 Symbol符号
// function函数 Object对象 Array数组 Set集合 Map键值对
// 可以迭代类型:通过for of可以遍历的
// String Array Set Map
var arr = ["我爱","我的","祖国"];
//默认遍历是 value
for(let item of arr){
console.log(item)
}
//keys 键名的集合
for(let key of arr.keys()){
console.log(key)
}
//value值的集合
for(let val of arr.values()){
console.log(val)
}
//entries键与值的集合
for(let [key,val] of arr.entries()){
console.log(key,val)
}
class 类
// 类:创造对象一个模板,
// 实例:是被创建的对象,一个类可以创建多个实例
// 狗类:名字,咬人,叫
class Animal {
constructor(name) {
this.name = name;
}
runing(){
console.log("我会走会跑,主人你在哪");
}
}
class Dog extends Animal{
constructor(name) {
super(name) //调用父类的构造函数
// this.name = name;
// this累的实例
}
// 构造函数 (new 类被调用)
bark(){
console.log("汪汪汪")
}
}
var d1 = new Dog("大黄");
var d2 = new Dog("小黑");
//猫类:名字,叫
class Cat extends Animal{
constructor(name,age=2) {
super(name); //调用父类的构造函数
this.age = age
// this.name = name;
}
bark(){
console.log("喵喵喵")
}
set Age(val){
if(val>0){
this.age = val;
}else{
console.log("设置年龄不正常");
}
}
get Age(){
console.log("被GET到了");
return this.age;
}
}
var c1 = new Cat("Tom");
var c2 = new Cat("Kity",5);