箭头函数 特性
箭头函数的特性
不绑定arguments,用rest参数…解决本身没有this的概念,捕获其所在上下文的 this 值作为自己的 this 值,this指向全局箭头函数不能使用new(会报错)箭头函数没有原型属性(prototype)箭头函数不能当做Generator函数,不能使用yield关键字箭头函数不能换行箭头函数有constructor、length属性箭头函数可以立即执行
改变this指向的方法有哪些
1)
在指定位置定义this存为变量
2)
使用箭头函数
3)
使用setTimeout
4)
使用call()方法
5)
使用bind()方法
6)
使用applay()方法
call(),bind(),apply()区别
1.apply、call、bind他们三个都能改变函数this的指向问题;
2.apply、call这两个方法的主动调用,bind返回的是改变this指向后的新函数;
3.传参的问题区别,call和bind都是直接传递参数,apply传递的是数组
Function.prototype.callNew = function(content,...sum){console.log(!content);if(!content|| context === null|| context === undefined){context = window;}let fn = Symbol();content[fn] = this;return content[fn](...sum);}
深拷贝
function deepClone(obj){let objClone = Array.isArray(obj)?[]:{};if(obj && typeof obj==="object"){for(key in obj){if(obj.hasOwnProperty(key)){//判断ojb子元素是否为对象,如果是,递归复制if(obj[key]&&typeof obj[key] ==="object"){objClone[key] = deepClone(obj[key]);}else{//如果不是,简单复制objClone[key] = obj[key];}}}}return objClone;} let a=[1,2,3,4],b=deepClone(a);a[0]=2;console.log(a,b);
原型
方法一:// Array.prototype.shifts = function(){// for(var i=0;i<this.length-1;i++){// this[i] = this[i+1];// }// this.length = this.length-1;// }// var arr = [11,22,33];// arr.shifts();// console.log(arr);
方法二:// Array.prototype.unshifts = function(user){// this.length = this.length+1;// for(var i=this.length-1;i>=0;i--){// this[i] = this[i-1];// }// this[0] = user;// }// var arr = [11,22,33];// arr.unshifts(44);// console.log(arr);
原型查找机制
- 当访问一个对象的属性或方法时,首先查找这个对象自身有没有
- 如果没有就查找它的原型(也就是 _ _ proto _ _指向的prototype 原型对象 )
- 如果还没有找到就查找原型对象的原型(Object的原型对象)
- 依次类推一直找到Object为止( null )
- _ _ proto _ _ 对象原型的意义就在于为对象成员查找机制提供一个方向,或者说一条线路