原生JS基础知识
this 笔试题
-
函数预编译过程 this --> window
function test() { console.log(this); } test();
-
全局作用域里 this --> window
-
call/apply 可以改变函数运行时this指向
-
obj.func(); func()里面的this指向obj
var person = { name: 'xiaoming', sayName: function () { console.log(this); console.log(this.name); } } person.sayName();
小知识点
-
Tips : ‘caller’, ‘callee’, and ‘arguments’ properties can not be accessed on strict mode functions and the arguments objects for calls to them
-
arguments.callee 指代当前函数的引用
function test() { console.log(arguments.callee); console.log(arguments.callee === test);// true }
function test() { console.log(arguments.callee); function demo() { console.log(arguments.callee); } demo(); } test();
// 应用 : 立即执行函数中使用递归写法 var num = (function(num) { if (num === 1) { return 1; } else { return num * arguments.callee(num - 1); } }(100)); console.log(num);
-
func.caller 指代当前函数被调用的环境
function test() { demo(); } function demo() { console.log(demo.caller); } test();