js高级-函数的四种调用模式
直接po代码和截图
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>JS高级-函数的四种调用模式</title>
<script type="text/javascript">
//
function add(num1, num2){
console.log(this);
return num1 + num1;
}
//第1种,函数执行模式
window.add(); //window可以省略不写
add(); // this === window //true this指的是window对象
//
function Cat(){
this.show = function(){
console.log(this);
}
}
//第2种,对象方法的调用模式
//所有的事件响应的方法都是对象方法调用模式
var c = new Cat();
// this === c //this指的是c对象
c.show();//对象调用自己的方法
//
function Dog(){
console.log(this);
this.address = "江西省赣州市于都县";
}
//第3种,构造器的调用模式
//构造器调用模式,this指向构造出来的对象
var d = new Dog(); // d === this //true
console.log(d.address);
//第4种,call和apply调用模式(暂时先放一放,以后有时间再讲)
</script>
</head>
<body>
<h1>JS高级-函数的四种调用模式</h1>
</body>
</html>