
函数的三种调用方式
第一种: 函数立即调用执行模式。这里面的this指向window;
function add(a,b){
console.log(this);
return a+b;
}
add();//this === window //true第二种:通过构造函数创建对象,然后调用自己的方法;这里的this指向对象本身;也可说是函数的调用者;
<script>
function fun(){
this.show=function(){
console.log(this);
}
}
var f=new fun();
f.show();//f对象;
</script>第三种:通过构造器调用函数:this指向构造出来的对象;
<script>
function Cat(){
console.log(this);
}
Cat.prototype.show=function(){
console.log(this);
}
var t=new Cat();//cat{};//通过构造函数创建的对象,相当于直接调用函数,没有返回值的情况下,得到的是cat本身;
t.show();//cat{}对象;
console.log(t==this);//false;
Cat.prototype.show();//show{};
Cat();//直接调用window
</script>想要了解web开发知识,请查阅 HTML中文网 !!
以上就是函数调用的三种方式是什么?的详细内容,更多请关注易知道|edz.cc其它相关文章!













