zoukankan      html  css  js  c++  java
  • 【JavaScript】JavaScript面试题1

    JavaScript面试题1

    作用域:

    题目1:

            var v = 123;
            function foo() {
                var v = 456;
                function inner() {
                    console.log(v)
                }
                return inner
            }
    
            result = foo();     
            console.log(result())   

    结果:

            var v = 123;
            function foo() {
                var v = 456;
                function inner() {
                    console.log(v)
                }
                return inner
            }
    
            result = foo();     //456
            console.log(result())   //undefined
    执行结果

    this的区别:

    题目1:

            Name = 'root';
            Age = 18;
            //function Foo(){}   函数名大写时一般是类
            function Foo(name,age) {
                this.Name=name;
                this.Age = age;
                this.Func = function () {
                    console.log('---',this.Name,this.Age);  
                    (function () {
                        console.log(this.Name,this.Age);    
                    })()
                }
            }
            obj = new Foo("Alex",666);
            obj.Func();  

    结果:

            Name = 'root';
            Age = 18;
            //function Foo 函数名大写时一般是类
            function Foo(name,age) {
                this.Name=name;
                this.Age = age;
                this.Func = function () {
                    console.log('---',this.Name,this.Age);  //Alex  666
                    (function () {
                        console.log(this.Name,this.Age);    //root  18
                    })()
                }
            }
            obj = new Foo("Alex",666);
            obj.Func();
    执行结果

    题目2:

            Name = 'alex';
            obj = {
                Name: 'root',
                Age: 18,
                Func: function () {
                    console.log(this);   
    
                    console.log(this.Name);  
                    var that = this;
    
                    function inner() {
                        console.log(this.Name);      
                        console.log(that.Name);      
                    }
                    inner();
    
                    // 自执行函数
                    (function () {
                        console.log(this.Name)  
                    })();
                }
            };
    
            obj.Func()    

    结果:

    Name = 'alex';
            obj = {
                Name: 'root',
                Age: 18,
                Func: function () {
                    console.log(this);   //this==obj
    
                    console.log(this.Name);  //root
                    var that = this;
    
                    function inner() {
                        //this = window
                        console.log(this.Name);      //alex
                        console.log(that.Name);      //root
                    }
                    inner();
    
                    // 自执行函数
                    (function () {
                        console.log(this.Name)  //alex
                    })();
                }
            };
    
            obj.Func()
    执行结果
  • 相关阅读:
    使用python,批量生产条形码
    excel——VlookUp函数的使用
    MQTT消息队列压力测试
    shell脚本中,关于if,以及条件判断
    linux下的echo
    python对文件操作 r w a 文件复制/修改
    使用Appium进行iOS的真机自动化测试
    pycharm 解决PEP8问题,配置autopep8到菜单栏
    Python中list的合并
    Centos最小化安装后,不能使用yum命令的解决办法
  • 原文地址:https://www.cnblogs.com/XJT2018/p/11365514.html
Copyright © 2011-2022 走看看