• 面试中的作用域题和THIS 指向的问题


    作用域的面试题

     1.

    fn()
    function fn () {
    console.log(12)
    }
    var as = function () {
    console.log(45)
    }

     2.

    var a = 12;
    function fn () {
    console.log(a)
    var a = 45;
    console.log(a)
    }
    fn()

    3.

    var a = 12;
    function fn () {
    console.log(a)
    a = 45;
    console.log(a)
    }
    fn()

    4.

    function fn () {
    console.log(11)
    function ff () {
    console.log(22)
    }
    ff()
    }
    fn()

    5.

    function fn () {
    console.log(5)
    fn()
    }
    fn()

    6.

    function fn () {
    console.log(12)
    }
    var as = fn()
    console.log(as)

    7.

    function fn () {
    console.log(12);
    return 45;
    console.log(456)
    }
    var as = fn()

    8.

    var a = 12;
    function fn () {
    console.log(a);
    return 4;
    var a = 45;
    }
    fn()

    9.

    var a = 45;
    function fn () {
    console.log(a)
    }
    fn()

    10.

    var a = 45;
    function fn (a) {
    console.log(a)
    }
    fn(5)

    11.

    var a = 123;
    function fun () {
    alert(a);
    }
    fun()

    12.

    var a = 123;
    function fun () {
    alert(a);
    var a = 456;
    }
    fun();
    alert(a);

    13.

    var a = 123;
    function fun () {
    alert(a);
    a = 456;
    }
    fun()
    alert(a)

    14.

    var a = 123;
    function fun (a) {
    alert(a)
    a = 456;
    }
    fun()
    alert(a)

    15.

    var a = 123;
    function fun (a) {
    alert(a)
    a = 456
    }
    fun()
    alert(a)
     

    16.

    function makeNoSense (x) {
    this.x = x;
    }
    makeNoSense(5);
    console.log(x);
    function test () {
    this.x = 1;
    alert(this.x);
    }
    test();
     
     

     

    This

    1.

    var name = '222';
    var a = {
    name : '111',
    say : function () {
    console.log(this.name)
    }
    }

    var fun = a.say;
    fun() //???
    a.say() //???

    var b = {
    name : '333',
    say : function (fun) {
    fun();
    }
    }
    b.say(a.say); //???
    b.say = a.say;
    b.say() //???
     

    2.

    var val = 1
    var obj = {
    val : 2,
    dbl : function() {
    var val = 45;
    console.log(this); //指向谁?
    this.val *= 2;
    console.log(this.val); //??
    console.log(val); //??
    }
    }
    var fn = obj.dbl
    fn()

    3.

    var x = 12;
    function test() {
    console.log(this.x)
    }
    test() //??

    4.

    var x = 12;
    function test() {
    console.log(this.x)
    }
    var obj={
    x:45,
    ss:test
    }
    obj.ss() //??

    5.

    var x = 12;
    function test() {
    console.log(this.x)
    }
    var obj = {
    x:45,
    ss:function(){
    console.log(this)
    test()
    }
    }
    obj.ss() //??

    6.

    var val = 1
    var obj = {
    val : 2,
    dbl : function() {
    // var val = 45;
    console.log(this); // 指向谁
    this.val *= 2;
    console.log(this.val); // ???
    console.log(val); // ???
    }
    }
    var ff = obj.dbl()

  • 相关阅读:

    转:在自己的工具条中使用ArcGIS Engine提供的命令和工具
    配置Subversion Apache TortoiseSVN
    vc++2005移除自定义向导目录
    NetAdvantage2006 For ASP.NET2.0印象
    三天了!今天终于成功安装ArcIMS9.2!
    vss 去除源代码管理
    转:设计模式趣谈
    转一篇:有关项目报告
    演练:使用 Visual Studio Team Test 进行单元测试
  • 原文地址:https://www.cnblogs.com/blankOne/p/10054601.html
Copyright © 2020-2023  润新知