zoukankan      html  css  js  c++  java
  • 网上面试资料整理

    整理网上的面试题

    一、去空格

    去除所有空格: str = str.replace(/s*/g,"");      
    
    去除两头空格: str = str.replace(/^s*|s*$/g,"");
    
    去除左空格: str = str.replace( /^s*/, “”);
    
    去除右空格: str = str.replace(/(s*$)/g, "");
    --------------------- 
    作者:wdlhao 
    来源:CSDN 
    原文:https://blog.csdn.net/wdlhao/article/details/79079660 
    版权声明:本文为博主原创文章,转载请附上博文链接!

    二、获取url中传入的参数

    <script>
            // 如何获取浏览器URL中查询字符串中的参数?
            // 测试地址为:http://www.runoob.com/jquery/misc-trim.html?channelid=12333&name=xiaoming&age=23
            
            function showWindowHref(){
                // var sHref = window.location.href;
                // 无法直接获取测试地址,直接以下面的字符串操作
                var sHref= 'http://www.runoob.com/jquery/misc-trim.html?channelid=12333&name=xiaoming&age=23;'
                var args = sHref.split('?');
                // console.log(args)
                var arrs = args[1].split('&')
                // console.log(arrs)
                var obj = {};
                for(let i = 0 , len = arrs.length ; i < len ; i++){
                    var arr = arrs[i].split('=')
                    console.log(arr)
                    obj[arr[0]] = arr[1];
                }
                return obj;
            }
    
            showWindowHref()
            /* 
                (2) ["channelid", "12333"]
                (2) ["name", "xiaoming"]
                (2) ["age", "23;"]
            */ 
    
        </script>
    --------------------- 
    作者:wdlhao 
    来源:CSDN 
    原文:https://blog.csdn.net/wdlhao/article/details/79079660 
    版权声明:本文为博主原创文章,转载请附上博文链接!
     

    三、this的应用

      1、普通函数,构造函数,箭头函数中this指向

    <body>
    <input type="button" id="text" value="点击一下" />
    </body>
    <script>
    // this的指向问题 // 普通函数,谁调用指向谁 function fn(){ console.log(this); } fn(); //Window // 构造函数,指向实例化的对象 function Fn(){ console.log(this); } new Fn(); //Fn {} // // 箭头函数 document.onclick = function(){ console.log(this) } //#document document.onclick = ()=>{ console.log(this) } //Window var btn = document.getElementById("text"); btn.onclick = function() { alert(this.value); //此处的this是按钮元素 }
    </script>

       2、apply 和 call 求数组最大值

        // 数组求最大值
            var arr1 = [55,66,33,45,61,99,38]
                // apply
            console.log(Math.max.apply(this,arr1))    //99
                // call
            console.log(Math.max.call(this,arr1))    //NAN
            console.log(Math.max.call(this,55,66,33,45,61,99,38))    //99

      总结:

    1、构造函数的this指向实例化后的那个对象
    2、普通函数的this指向调用该函数的那个对象
    3、箭头函数的this指向创建时的那个对象,而不是引用时的那个对象
    
    4、通过apply,call可以求数组中的最大值
        语法如下:
            Math.max.apply(this,arr)

    如有问题,请与本人联系,立即删除

  • 相关阅读:
    LeetCode-20.Valid Parentheses
    LeetCode-2. Add Two Numbers
    LeetCode-1. Two Sum
    LeetCode Top100 Liked Questions
    位运算
    sublime中文乱码解决办法
    jQuery 参考手册
    jQuery 参考手册
    jQuery 参考手册
    《锋利的Jquery第二版》读书笔记 第三章
  • 原文地址:https://www.cnblogs.com/-roc/p/9987293.html
Copyright © 2011-2022 走看看