前端基础知识练习题---This 练习题

本次考试为闭卷考试,切勿翻书📖,上网🌐,也不要打开 F12,更不要退出页面,使用本地 IDEA。

请参加考试的人,自觉遵守考试规则。❗️❗️❗️❗️❗️

第一题:

var foo = 123;
function print() {
  this.foo = 234;
  console.log(this);
  console.log(foo);
}
print();

第二题:

"use strict";
var foo = 123;
function print() {
  console.log('print this is ', this);
  console.log(window.foo)
  console.log(this.foo);
}
console.log('global this is ', this);
print();

第三题:

let a = 1;
const b = 2;
var c = 3;
function print() {
  console.log(this.a);
  console.log(this.b);
  console.log(this.c);
}
print();
console.log(this.a);

第四题:

a = 1;
function foo() {
  console.log(this.a);
}
const obj = {
  a: 10,
  bar() {
    foo();
  }
}
obj.bar();

第五题:

var a = 1
function outer() {
  var a = 2
  function inner() {
    console.log(this.a) // 1
  }
  inner()
}
outer()

第六题:

a = 1;
(function () {
  console.log(this);
  console.log(this.a)
}())
function bar() {
  b = 2;
  (function () {
    console.log(this);
    console.log(this.b)
  }())
}
bar();

第七题:

var a = 1;
function foo() {
  console.log(this.a);
}
var obj = { a: 2, foo }
foo();
obj.foo();

第八题:

var obj1 = {
  a: 1,
  obj2: {
    a: 2,
    foo() {
      console.log(this.a)
    }
  }
}
obj1.obj2.foo()

第九题:

a = 1
var obj = {
  a: 2,
  foo() {
    console.log(this.a)
  }
}
var foo = obj.foo;
obj.foo();
foo();

第十题:

var obj = {
  a: 1,
  foo() {
    console.log(this.a)
  }
};
var a = 2;
var foo = obj.foo;
var obj2 = { a: 3, foo: obj.foo }

obj.foo();
foo();
obj2.foo();

第十一题:

function foo() {
  console.log(this.a)
}
function doFoo(fn) {
  console.log(this)
  fn()
}
var obj = { a: 1, foo }
var a = 2
doFoo(obj.foo)

第十二题:

function foo() {
  console.log(this.a)
}
function doFoo(fn) {
  console.log(this)
  fn()
}
var obj = { a: 1, foo }
var a = 2
var obj2 = { a: 3, doFoo }
obj2.doFoo(obj.foo)

第十三题:

var name = 'zcxiaobao';
function introduce() {
  console.log('Hello,My name is ', this.name);
}
const Tom = {
  name: 'TOM',
  introduce: function () {
    setTimeout(function () {
      console.log(this)
      console.log('Hello, My name is ', this.name);
    })
  }
}
const Mary = {
  name: 'Mary',
  introduce
}
const Lisa = {
  name: 'Lisa',
  introduce
}

Tom.introduce();
setTimeout(Mary.introduce, 100);
setTimeout(function () {
  Lisa.introduce();
}, 200);

第十四题:

name = 'javascript';
let obj = {
  name: 'obj',
  A() {
    this.name += 'this';
    console.log(this.name)
  },
  B(f) {
    this.name += 'this';
    f();
  },
  C() {
    setTimeout(function () {
      console.log(this.name);
    }, 1000);
  }
}
let a = obj.A;
a();
obj.B(function () {
  console.log(this.name);
});
obj.C();
console.log(name);

第十五题:

function foo() {
  console.log(this.a)
}
var obj = { a: 1 }
var a = 2

foo()
foo.call(obj)
foo.apply(obj)
foo.bind(obj)

第十六题:

var obj1 = {
  a: 1
}
var obj2 = {
  a: 2,
  bar: function () {
    console.log(this.a)
  },
  foo: function () {
    setTimeout(function () {
      console.log(this)
      console.log(this.a)
    }.call(obj1), 0)
  }
}
var a = 3
obj2.bar()
obj2.foo()

第十七题:

function foo() {
  console.log(this.a)
}
var obj = { a: 1 }
var a = 2

foo()
foo.call(obj)
foo().call(obj)

第十八题:

function foo() {
  console.log(this.a)
  return function () {
    console.log(this.a)
  }
}
var obj = { a: 1 }
var a = 2

foo()
foo.call(obj)
foo().call(obj)

第十九题:

function foo() {
  console.log(this.a)
  return function () {
    console.log(this.a)
  }
}
var obj = { a: 1 }
var a = 2

foo()
foo.bind(obj)
foo().bind(obj)

第二十题:

function foo() {
  console.log(this.a)
  return function () {
    console.log(this.a)
  }
}
var obj = { a: 1 }
var a = 2
foo.call(obj)()

第二十一题:

var obj = {
  a: 'obj',
  foo: function () {
    console.log('foo:', this.a)
    return function () {
      console.log('inner:', this.a)
    }
  }
}
var a = 'window'
var obj2 = { a: 'obj2' }

obj.foo()()
obj.foo.call(obj2)()
obj.foo().call(obj2)

第二十二题:

var obj = {
  a: 1,
  foo: function (b) {
    b = b || this.a
    return function (c) {
      console.log(this.a + b + c)
    }
  }
}
var a = 2
var obj2 = { a: 3 }

obj.foo(a).call(obj2, 1)
obj.foo.call(obj2)(1)

第二十三题:

function User(name, age) {
  this.name = name;
  this.age = age;
}
var name = 'Tom';
var age = 18;

var zc = new User('zc', 24);
console.log(zc.name)

第二十四题:

function User(name, age) {
  this.name = name;
  this.age = age;
  this.introduce = function () {
    console.log(this.name)
  }
  this.howOld = function () {
    return function () {
      console.log(this.age)
    }
  }
}
var name = 'Tom';
var age = 18;
var zc = new User('zc', 24)
zc.introduce()
zc.howOld()()

第二十五题:

name = 'tom'
const obj = {
  name: 'zc',
  intro: () => {
    console.log('My name is ' + this.name)
  }
}
obj.intro()

第二十六题:

name = 'tom'
const obj = {
  name: 'zc',
  intro: function () {
    return () => {
      console.log('My name is ' + this.name)
    }
  },
  intro2: function () {
    return function () {
      console.log('My name is ' + this.name)
    }
  }
}
obj.intro2()()
obj.intro()()

第二十七题:

name = 'window'
const obj1 = {
  name: 'obj1',
  intro: function () {
    console.log(this.name)
    return () => {
      console.log(this.name)
    }
  }
}
const obj2 = {
  name: 'obj2',
  intro: () => {
    console.log(this.name)
    return function () {
      console.log(this.name)
    }
  }
}
const obj3 = {
  name: 'obj3',
  intro: () => {
    console.log(this.name)
    return () => {
      console.log(this.name)
    }
  }
}

obj1.intro()()
obj2.intro()()
obj3.intro()()

第二十八题:

function User(name, age) {
  this.name = name;
  this.age = age;
  this.intro = function () {
    console.log('My name is ' + this.name)
  };
  this.howOld = () => {
    console.log('My age is ' + this.age)
  }
}

var name = 'Tom', age = 18;
var zc = new User('zc', 24);
zc.intro();
zc.howOld();

第二十九题:

var name = 'window'
var obj1 = {
  name: 'obj1',
  intro: function () {
    console.log(this.name)
    return () => {
      console.log(this.name)
    }
  },
  intro2: () => {
    console.log(this.name)
    return function () {
      console.log(this.name)
    }
  }
}
var obj2 = {
  name: 'obj2'
}
obj1.intro.call(obj2)()
obj1.intro().call(obj2)
obj1.intro2.call(obj2)()
obj1.intro2().call(obj2)

第三十题:

var name = 'window'
var user1 = {
  name: 'user1',
  foo1: function () {
    console.log(this.name)
  },
  foo2: () => console.log(this.name),
  foo3: function () {
    return function () {
      console.log(this.name)
    }
  },
  foo4: function () {
    return () => {
      console.log(this.name)
    }
  }
}
var user2 = { name: 'user2' }

user1.foo1()
user1.foo1.call(user2)

user1.foo2()
user1.foo2.call(user2)

user1.foo3()()
user1.foo3.call(user2)()
user1.foo3().call(user2)

user1.foo4()()
user1.foo4.call(user2)()
user1.foo4().call(user2)

第三十一题:

var x = 10;
var foo = {
  x: 20,
  bar: function () {
    var x = 30;
    console.log(this.x)

  }
};
foo.bar();
(foo.bar)();
(foo.bar = foo.bar)();
(foo.bar, foo.bar)();

第三十二题:

var length = 10;
function fn() {
  console.log(this.length);
}

var obj = {
  length: 5,
  method: function (fn) {
    fn();
    arguments[0]();
  }
};

obj.method(fn, 1);

第三十三题:

var number = 5;
var obj = {
  number: 3,
  fn: (function () {
    var number;
    this.number *= 2;
    number = number * 2;
    number = 3;
    return function () {
      var num = this.number;
      this.number *= 2;
      console.log(num);
      number *= 3;
      console.log(number);
    }
  })()
}
var myFun = obj.fn;
myFun.call(null);
obj.fn();
console.log(window.number);

你以为拉到底部就有答案了?

在这里插入图片描述

小伙子,你太天真啦

在这里插入图片描述

题目的答案及解析,不再这里哦
什么时候有?
看我心情吧

在这里插入图片描述

特此声明:本次题目来源:
《2w字大章 38道面试题》彻底理清JS中this指向问题
【建议👍】再来40道this面试题酸爽继续(1.2w字用手整理)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值