一些讲运算结果类型的面试题【不定时更新】
1 2 3 4 5 6 7 8 9 10 11
| var name = "Hello"; var object = { name: "world", getNameFunc: function() { return function() { return this.name; }; } };
console.log(object.getNameFunc()());
|
object.getNameFunc() 返回的就是一个 function,function () { return this.name }
1 2 3 4 5 6 7 8 9 10 11 12
| var name = "Hello"; var object = { name: "world", getNameFunc: function() { var that = this; return function() { return that.name; }; } };
console.log(object.getNameFunc()());
|
这个 that = this
产生了闭包
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| function fun(n, o) { console.log(o); return { fun: function(m) { return fun(m, n); } }; } var a = fun(0); a.fun(1); a.fun(2); a.fun(3);
var b = fun(0).fun(1).fun(2).fun(3);
var c = fun(0).fun(1); c.fun(2); c.fun(3);
|
作用域
1 2 3 4 5 6 7 8 9
| var x = 10; function fn() { console.log(x); } function show(f) { var x = 20; f(); } show(fn);
|
函数的作用域, 跟函数的调用位置的作用域没关系
1 2 3 4 5 6 7 8 9 10 11 12
| var fn = function() { console.log(fn); }; fn();
var obj = { fn2: function() { console.log(fn2); } };
obj.fn2();
|