this题目:
第一题:
var myObject = {
foo : "bar",
fun : function(){
var self = this;
console.log("outer fun: this.foo = " + this.foo);
console.log("outer fun: self.foo = " + self.foo);
(function(){
console.log("inner fun : this.foo = " + this.foo);
console.log("outer fun: self.foo = " + self.foo);
})();
}
}
myObject.fun();
第二题:
var hero = {
_name: "wuwei",
getName: function (){
return this._name;
}
}
var stoleName = hero.getName;
console.log(stoleName());
console.log(hero.getName());
第三题:
var length = 10;
function fn(){
console.log(this.length);
}
var obj = {
length: 5,
method: function(fn){
fn();
arguments[0]();
}
}
obj.method(fn,1);
第四题:
function a(){
y = function(){
x = 2;
};
return function(){
var x = 3;
y();
console.log(this.x);
}.bind(this)
}
a();
第五题:
var n = 10;
var obj = {
n: 5,
c: a()
};
function a(){
var n = 7;
var c = function(){
var n = 3;
console.log(this.n);
}
return c;
}
obj.c(); // 5
对象题目
第六题:
var a = {},
b = {name:"b"},
c = {name:"c"};
a[b] = 123;
a[c] = 456;
console.log(a[b]);
第七题:
var a = {n:1};
var b = a;
a.n = a = {m:1}
console.log(a);
console.log(b)