javascript中调用function经常使用两种javascript
一、做为一个函数直接调用java
function test(){ alert("test"); } test();
二、做为对象的属性方法调用windows
var instance = { name : "test", getName = function(){ alert(this.name); } } instance.getName();
他们不一样之处就是函数调用的上下文对象不一样,(既是this),第一个就是在全局做用域下的调用,此时刻的this 就是windows对象,第二种的this就是对象自己既instance;函数
补充:this
匿名函数的调用就是否是基于任何一个对象的方法,顾它的this就全局的对象windowcode
var instance = { name :"test", getName:function(){ console.log(this.name); (function(){ console.log(this == window); })() } } instance.getName();