一、this关键字指向问题
函数正常调用的时候this指向window
<script>var name = "张三";function show(){console.log(this.name)}function go(){var name = "李四"show();}go(); //张三window.go(); //张三</script>
call、apply、bind、可以改变函数内部this关键字的指向
functionName.call(obj)
functionName.apply(obj)
<script>var name = "王五";var obj ={name :"李四"}function go(){console.log(this.name)}go(); //王五go.call(obj); //李四go.apply(obj); //李四</script>
call与apply之间的区别:传递多个参数时,call依次去传递;apply需要传递数组
<script>var name = "window";function show(a,b){console.log(this.name);console.log(a+b)}var zhang = {name:"张三"}show(1,2) //window 3show.call(zhang,1,2) //张三 3show.apply(zhang,[1,2]); //张三 3</script>
bind绑定的函数不会马上执行,只是改变了函数上下文的执行环境
call、apply调用的时候马上执行
<script>var name = "window";var zhang = {name:"张三"}var show=function(a,b){console.log(this.name);console.log(a+b)}.bind(zhang)show(2,3); //张三 5</script>
eg:bind在定时器中的几种形式 & 箭头函数改变this指向
问题:
<body><button id="btn">按钮</button><script>var id = "1001";var btn = document.getElementById("btn");btn.onclick = function(){setTimeout(function(){console.log(this.id) //1001},1000)}</script></body>
方法:
形式1:
<body><button id="btn">按钮</button><script>var id = "1001";var btn = document.getElementById("btn");btn.onclick = function(){setTimeout(function(){console.log(this.id) //btn}.bind(btn),1000)}</script></body>
形式2:
<body><button id="btn">按钮</button><script>var id = "1001";var btn = document.getElementById("btn");btn.onclick = function(){setTimeout(function(){console.log(this.id) //btn}.bind(this),1000)}</script></body>
形式3:
<body>
<button id="btn">按钮</button>
<script>
var id = "1001";
var btn = document.getElementById("btn");
btn.onclick = function(){
var self = this;
setTimeout(function(){
console.log(this.id) //btn
}.bind(self),1000)
}
</script>
</body>
箭头函数:
<body>
<button id="btn">按钮</button>
<script>
var id = "1001";
var btn = document.getElementById("btn");
btn.onclick = function(){
setTimeout(()=>{
console.log(this.id) //btn
},1000)
}
</script>
</body>
二、继承
属性的继承
call能实现属性的继承:
<script>
function Person(name,age){
this.name = name;
this.age = age;
}
function Teacher(name,age,skill){
Person.call(this,name,age)
this.skill =skill;
}
var t = new Teacher("zhang",18,"js");
console.log(t); //Teacher {name: "zhang", age: 18, skill: "js"}
</script>
方法的继承
子(Teacher).prototype = 父(Person).prototype;
<script>
function Person(name,age){
this.name = name;
this.age = age;
}
Person.prototype.sayName = function(){
console.log(this.name)
}
function Teacher(name,age,skill){
Person.call(this,name,age)
this.skill = skill;
}
Teacher.prototype = Person.prototype; //方法的继承
Teacher.prototype.constructor = Teacher; //重置构造属性
Teacher.prototype.sayAge = function(){
console.log(this.age)
}
var t = new Teacher("zhang",18,"js");
var p = new Person("lisi",13);
console.log(t.constructor)
t.sayName(); //zhang
t.sayAge(); //18
</script>
子(Teacher).prototype = Object.create(父(Person).prototype,{
constructor:{
value:子(Teacher)
}
})
<script>
function Person(name,age){
this.name = name;
this.age = age;
}
Person.prototype.sayName = function(){
console.log(this.name)
}
function Teacher(name,age,skill){
Person.call(this,name,age)
this.skill =skill;
}
Teacher.prototype = Object.create(Person.prototype,{ //方法的继承&重置构造属性
constructor:{
value:Teacher
}
})
Teacher.prototype.sayAge = function () {
console.log(this.age)
}
var t = new Teacher("zhang",18,"js");
console.log(t.constructor);
t.sayName(); //zhang
t.sayAge(); //18
</script>
