87,991
社区成员




var myObject = {
foo:"bar",
func: function() {
var self = this;
console.log("outer func: this.foo = " + this.foo);
console.log("outer func: self.foo = " + self.foo);
(function (){
console.log("inner func: this.foo = " + this.foo);
console.log("inner func: self.foo = " + self.foo);
}());
}
}
myObject.func();
function outer(){
this.i = 10;
alert(this.i);
(function(){
alert(this.i);
}());
};
outer();
var a = function (){
console.log("a");
}
b = a;
a(); //a
b(); //a
delete a;
//这个时候,a和b都指向的同一个函数引用。
a = function(){
console.log("b");
}
//改变a的函数引用,但是b依然指向原来的函数
a(); //b
b(); //a
//所以说,函数在被定义之后,就会一直存在,不管你是否引用。
var myObject = {
foo:"bar",
func: function() {
var self = this;
console.log("outer func: this.foo = " + this.foo);
console.log("outer func: self.foo = " + self.foo);
function aa(){
console.log("inner func: this.foo = " + this.foo);
console.log("inner func: self.foo = " + self.foo);
}
//这个跟是不是匿名的自执行函数没有太大关系吧,而是跟function的内部this执向有关的。
//function aa(){}这样的函数定义,内部的this默认都是指向window的,可以通过其他方式改变
//所以下面的调用方法,this=window
aa();
//而下面的调用方法,则改变了this的指向
aa.call(this);
}
}
myObject.func();
//例子:
var name = "zhang";
function getName(){
console.log(this.name);
}
var obj = {
name:"ling",
getName:getName
}
getName();//zhang 直接调用,this=window
getName.call(obj); //ling 使用call强行把getName内部的this指向obj
obj.getName();//ling 在obj上调用,指向this=obj
//上面的这些,只是想要说明,this的指向,跟它的使用方法有关。
//跟这个函数,定义在哪个区域无关。
var age = 2;
var MyAge = {
age:1,
getAge:function(){
console.log(this.age);
}
}
var newAge = MyAge.getAge;
MyAge.getAge(); //1 this指向MyAge
newAge(); //2 this指向window
newAge.call(MyAge); //1 this指向MyAge
function outer(){
this.i = 10;
alert(this.i);
(function(){
alert(this.i);
}());
};
outer();
你的这个调用的时候没new,没new的,都是this都是指向window,所以匿名函数里可以找到i。你new outer()就是undefined了、