<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var o = {
sayHi: function(){
console.log('我是改变后this指向');
}
}
function fn(a,b){
console.log(this);
console.log(a+b);
}
fn.call(o,1,3);
// call第一个可以调用函数,第二个可以改变函数内的this指向
// call的只要作用可以实现继承
function Father(uanme,age,sex){
this.uanme = uanme;
this.age = age;
this.sex = sex;
}
function Son(uname,age,sex){
Father.call(this,uname,age,sex);
}
var son = new Son('ann',18,'nv');
console.log(son);
</script>
</body>
</html>
改变函数内this指向问题
最新推荐文章于 2024-04-04 19:30:18 发布