this指向练习题(一):

2018-09-05  本文已影响0人  锋享前端

亲爱的小伙伴们,大家都知道在js中this指向对于小白来说是很难搞定的,大家可以看看
this指向,然后做一做下面的9道练习题:看看自己是不是掌握了this指向问题;
// 1

function a() {
    var user = "剃了胡子";
    console.log(this.user);
}
a();

// 2

var o = {
    user: "剃了胡子"",
    fn: function () {
        console.log(this.user);
        console.log(this);
    }
}
o.fn();

// 3

var o = {
    a: 10,
    b: {
        //a:12,
        fn: function () {
            console.log(this.a);
        }
    },
    fn1: function () {
        console.log(this.a);
    }
}
o.fn1();
o.b.fn();

// 4

function fn() {
    this.user = '剃了胡子"';
    return {};
}
var a = new fn;
console.log(a.user);

// 5

function fn() {
    this.user = '剃了胡子"';
    return function () {};
}
var a = new fn;
console.log(a.user);

// 6

function fn() {
    this.user = '剃了胡子"';
    return 1;
}
var a = new fn;
console.log(a.user);

// 7

function fn() {
    this.user = '剃了胡子"';
    return undefined;
}
var a = new fn;
console.log(a.user);

// 8

function fn() {
    this.user = '剃了胡子"';
    return null;
}
var a = new fn;
console.log(a.user);

// 9

var o = {
    a: 10,
    b: {
        a: 12,
        fn: function () {
            console.log(this.a);
            console.log(this);
        }
    }
}
var j = o.b.fn;
j();

大家用了多久做完的呢?

上一篇下一篇

猜你喜欢

热点阅读