zoukankan      html  css  js  c++  java
  • JS常见知识点

    1、写一个按照下面方式调用都能正常工作的 sum 方法

    console.log(sum(2,3)); // Outputs 5
    console.log(sum(2)(3)); // Outputs 5

    解决方案

    function sum() {
      var fir = arguments[0];
      if (arguments.length === 2) {
        return arguments[0] + arguments[1]
      } else {
        return function (sec) {
          return fir + sec;
        }
      }
    }
    sum(2)(5)

    2、下面的代码会输出什么?为什么?

    var arr1 = "john".split(''); j o h n
    var arr2 = arr1.reverse(); n h o j
    var arr3 = "jones".split(''); j o n e s
    arr2.push(arr3);
    console.log("array 1: length=" + arr1.length + " last=" + arr1.slice(-1));
    console.log("array 2: length=" + arr2.length + " last=" + arr2.slice(-1));
    • reverse() 会改变数组本身,并返回原数组的引用。
    • slice() 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分。
      stringObject.slice(start,end);
      arrayObject.slice(start,end);
      • start和end如果是负数,则该参数规定的是从字符串的尾部开始算起的位置。也就是说,-1 指字符串的最后一个字符,-2 指倒数第二个字符,以此类推。 

    3、下面的代码会输出什么?为什么?

    1 console.log(1 + "2" + "2");   
    2 console.log(1 + +"2" + "2");
    3 console.log(1 + -"1" + "2");
    4 console.log(+"1" + "1" + "2");
    5 console.log( "A" - "B" + "2");
    6 console.log( "A" - "B" + 2);

      我们先来看几条规则:

    • 数字字符串之前存在数字中的正负号(+/-)时,会被转换成数字
      console.log("类型:"+(typeof '3')+" 内容为:"+'3')      //类型:string 内容为:3
      console.log("类型:"+(typeof +'3')+" 值为:"+(+'3'))    //类型:number 值为:3
      console.log("类型:"+(typeof -'3')+" 值为:"+(-'3'));  //类型:number 值为:-3
    • (字符串 + 数字) 或者 (数字+字符串) ,都得到一个字符
      console.log(5 + 'A');    //5A 
      console.log('A' + 5);    //A5
      console.log(NaN + 'A'); //NaNA
      console.log('A' + NaN); //ANaN
    • 对于运算结果不能转换成数字的,将返回 NaN
      console.log('a' * 'sd'); //NaN
      console.log('A' - 'B'); // NaN
    • 其他的一些例子
      console.log('-3' - 2 + 1);    //-4   number类型
      console.log( 3 + '-2' + 1);   //3-21 string类型
      console.log( 3 - '-2' + 1);   //6    number类型
    • 也许我们已经有答案了
      1 console.log(1 + "2" + "2");      // "122"
      2 console.log(1 + +"2" + "2");     // "32"
      3 console.log(1 + -"1" + "2");     // "02"
      4 console.log(+"1" + "1" + "2");   //"122"
      5 console.log( "A" - "B" + "2");  // "NaN2"
      6 console.log( "A" - "B" + 2);    //NaN

       (未完待续)

  • 相关阅读:
    VS2019正式版 密钥 Visual Studio 2019 破解 激活码 Key
    关于随机数的前世今生
    木兮的纪中集训感想
    浅谈欧洲算法——模拟退火
    你没听过的梅森旋转算法
    二分贪心杂题
    DP专项训练
    实验八 进程间通信
    实验七 信号
    实验六 进程基础
  • 原文地址:https://www.cnblogs.com/HXW-from-DJTU/p/5952081.html
Copyright © 2011-2022 走看看