美文网首页
this的指向的面试题

this的指向的面试题

作者: 疯狂的蜗牛Dianna | 来源:发表于2020-05-18 12:20 被阅读0次
面试题1
        var age = 38;
        console.log(window);
        var obj = {
            age: 18,
            getAge: function(){
                console.log(this.age);
            }
        }
        obj.getAge();
        // obj => 18
        var f = obj.getAge;
        f();
         // window => 38
面试题2
        var age = 38;
        var obj = {
            age: 18,
            getAge: function(){
                console.log(this.age);
                function foo(){
                    console.log(this.age);
                }
                foo();
            }
        }
        obj["getAge"]();
        // 18 38
面试题3
var length = 10;
var age = 18;
function fn(){
console.log(this.length);
}
var arr = [fn, "222"];
fn();
arr[0]();
面试题4
        var length = 10;
        var age = 18;
        function fn(){
            console.log(this.length);
        }
        var arr = [fn,"222"]
        fn();
        arr[0]();

相关文章

网友评论

      本文标题:this的指向的面试题

      本文链接:https://www.haomeiwen.com/subject/xozyohtx.html