es6-2-apply call、map、set、for of

<html>
<head>
	<title></title>
</head>
	<body>
	<!--  
		6.call apply --改变当前对象指向
			Function.apply(obj,argsArr) --Obj代替了function里的this,argsArr接收的是数组[1,2,3],作为参数
			Function.call(obj,args) --obj代替了Funciton里的this,args接收的是1,2,3,作为参数
		7.Set --一种新的数据结构,类似于数组,值是唯一的,通过Set构造函数创建
			size:返回成员个数
			add:与数组的push一样,加数据
			delete:删除,直接删除某个值
			has:是否有某个值 true false
			clear:清楚所有,清空
			for of 循环
			forEach(不常用)
			keys-键 
			values-值
			entries-返回键值对
		8.Map --一种新的数据结构,类似于对象,属性名不限制类型,通过Map构造函数来创建,接受一个数组当参数,数组里面是若干数组
			set:创建空Map(),map.set创建属性名和属性值
			get('属性名'):取值
			has('属性名'):是否有某个属性 返回true false
			delete('属性名'):删除某个属性 返回true false
			size:返回Map里有多少成员
			clear():清除
			keys():取属性名 values():去属性值 entries():取键值对
			for of 循环
			forEach(不常用)

	-->
	<script type="text/javascript">
		//创建对象:创建自变量,构造函数
		//箭头函数-案例  --不能直接当属性值
		//函数不是对象实例,所以用函数创建一个对象
		function Timer(){
			this.s1=0; //window的
			this.s2=0; //window的
			//定时器,每一秒运行一次
			setInterval(()=>{this.s1++},1000);  //直接用全局
			setInterval(function(){this.s2++},1000);  //因为这个this外面还阔了一个函数,所以无法直接使用
		}
		var timer = new Timer();
		//一次性定时器,每三秒运行一次
		setTimeout(()=>{console.log('s1',timer.s1)},3000);
		//结果:3 --在3秒里运行了3次,返回值是最终结果3
		setTimeout(()=>{console.log('s2',timer.s2)},3000);
		//结果:0 --没有调用,window设定this.s2=0


		//apply call  --改变当前对象指针
		function method1(arg1,arg2){
			return arg1+arg2
		}
		function method2(arg1,arg2){
			return arg1-arg2
		}
		var res1 = method2.apply(method1,[3,2])
		console.log(res1)  //结果:1
		var res2 = method1.call(method2,3,2)
		console.log(res2)  //结果:5

		//apply call --this改变当前对象指针
		function father(word){
			this.word = word;
			this.showName1=function(){
				console.log("father say:",this.word)
			}
		}
		function mother(word){
			this.word = word;
			this.showName2=function(){
				console.log("mother say:",this.word)
			}
		}
		function child(word){
			father.call(this,word); 
			//这个this.word替代了father里面的this.word
			mother.call(this,word);
		}
		var c = new child("boys");
		c.showName1()
		//用child方法调用father这个方法,然后把child里面的参数传给father
		//就是c的参数传给里面的方法进行调用



		//apply call --覆盖问题
		function father1(word,word1){
			this.word = word1;
			this.showName3 = function(){
				console.log("dad says:",this.word)
			}
		}
		function child1(word,word1){
			father1.call(this,word,word1);
			// mother1.call(this,word,word1);
		}
		var c1 = new child1("boys","girls")
		c1.showName3()
		//结果:father says: girls

		//--------加上下面的会被覆盖------------
		function mother1(word,word1){
			this.word = word;
			this.showName4 = function(){
				console.log("mother says:",this.word)
			}
		}
		c1.showName3()
		//结果:father says: boys
		//因为用的是c的参数,所以在方法里c的参数在使用的方法里一直在改变
		//在father方法里将this.word覆盖为word1,
		//在mother方法里把原本的word1又该为了word,所以结果是 father says:boys


		//Math不接受数组,所以将null代替Math里面的this也可以实现
		var a=Math.max.apply(null,[2,3,4,9])
		console.log(a)
		//结果:9


		//Set --可以实现数组去重
		var set = new Set([1,2,1,1,2,3])
		console.log(set)
		//结果:{1,2,3}
		arr=[...set]  // --将{}转换为数组结构
		console.log(arr)
		//结果:[1,2,3]
		console.log(set.size)
		//结果:3
		console.log(set.add(6))
		//结果:{1,2,3,6}
		console.log(set.add(6).add(5))
		//结果:{1,2,3,6,5}
		console.log(set.delete(5))
		console.log(set.clear())


		//for of 循环
		var arr1 = [1,2,2,3,4]
		var set = new Set(arr1);
		for(var item of set.keys()){
			console.log(item)
		}
		for(var item of set.entries()){
			console.log(item)
			//结果:[1,1],[2,2],[3,3],[4,4]
		}


		//Map --类对象{属性名=>属性值},数组套数组
		var map = new Map([['name','张三'],['title','Author']])
		console.log(map)
		//结果:{'name'=>'张三','title'=>'Author'}
		var map1 = new Map([[[1,2,3],'张三'],['tit','au']])
		console.log(map1)
		//结果:{[1,2,3]=>'张三','tit'=>'au'}

		//Map和set创建 属性名 和 属性值
		const map2 = new Map();
		map2.set('aaa','123')  // --只能传两个,多了不显示
		console.log(map2)
		//结果:{'aaa'=>'123'}
		console.log(map2.get('aaa'))
		//结果:123
		console.log(map2.size)
		//结果:1
		console.log(map.clear())
		//结果:undefined

		for(let item of map1.keys()){
			console.log(item)
			//结果:[1,2,3]
			//		tit
		}
		for(let item of map1.values()){
			console.log(item)
			//结果:张三
			//		au
		}

	</script>

</body>
</html> 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值