zoukankan      html  css  js  c++  java
  • js 不常用面试题 数组对象深度取值

    function getPersonInfo(one, two, three) {
      console.log(one);
      console.log(two);
      console.log(three);
    }
    
    const person = "Lydia";
    const age = 21;
    
    getPersonInfo`${person} is ${age} years old`;
    A: Lydia
    21 ["", "is", "years old"] B: ["", "is", "years old"] Lydia 21 C: Lydia ["", "is", "years old"] 21 答案 B 如果使用标记的模板字符串,则第一个参数的值始终是字符串值的数组。 其余参数获取传递到模板字符串中的表达式的值!
    let a = 3;
    let b = new Number(3);
    let c = 3;
    
    console.log(a == b);
    console.log(a === b);
    console.log(b === c);
    
    A:
    true false true B: false false true C: true false false D: false true true 答案: C
    class Chameleon {
      static colorChange(newColor) {
        this.newColor = newColor;
      }
    
      constructor({ newColor = "green" } = {}) {
        this.newColor = newColor;
      }
    }
    
    const freddie = new Chameleon({ newColor: "purple" });
    freddie.colorChange("orange");
    
    A: orange B: purple C: green D: TypeError 答案: D
    function Person(firstName, lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
    }
    
    const lydia = new Person("Lydia", "Hallie");
    const sarah = Person("Sarah", "Smith");
    
    console.log(lydia);
    console.log(sarah);
    
    A: Person {firstName:
    "Lydia", lastName: "Hallie"} and undefined B: Person {firstName: "Lydia", lastName: "Hallie"} and Person {firstName: "Sarah", lastName: "Smith"} C: Person {firstName: "Lydia", lastName: "Hallie"} and {} D:Person {firstName: "Lydia", lastName: "Hallie"} and ReferenceError 答案: A 对于sarah,我们没有使用new关键字。 使用new时,它指的是我们创建的新空对象。 但是,如果你不添加new它指的是全局对象! 我们指定了this.firstName等于'Sarah和this.lastName等于Smith。 我们实际做的是定义global.firstName ='Sarah'和global.lastName ='Smith。 sarah本身的返回值是undefined。
    const obj = { 1: "a", 2: "b", 3: "c" };
    const set = new Set([1, 2, 3, 4, 5]);
    
    obj.hasOwnProperty("1");
    obj.hasOwnProperty(1);
    set.has("1");
    set.has(1);
    
    A:
    false true false true B: false true true true C: true true false true D: true true true true 答案: C 所有对象键(不包括Symbols)都会被存储为字符串,即使你没有给定字符串类型的键。 这就是为什么obj.hasOwnProperty('1')也返回true。 上面的说法不适用于Set。 在我们的Set中没有“1”:set.has('1')返回false。 它有数字类型1,set.has(1)返回true。
    const a = {};
    const b = { key: "b" };
    const c = { key: "c" };
    
    a[b] = 123;
    a[c] = 456;
    
    console.log(a[b]);
    
    A:
    123 B: 456 C: undefined D: ReferenceError 答案: B 对象键自动转换为字符串。我们试图将一个对象设置为对象a的键,其值为123。 但是,当对象自动转换为字符串化时,它变成了[Object object]。 所以我们在这里说的是a["Object object"] = 123。 然后,我们可以尝试再次做同样的事情。 c对象同样会发生隐式类型转换。那么,a["Object object"] = 456。
    0;
    new Number(0);
    ("");
    (" ");
    new Boolean(false);
    undefined;
    
    A:
    0, '', undefined B: 0, new Number(0), '', new Boolean(false), undefined C: 0, '', new Boolean(false), undefined D: 所有都是假值 答案: A JavaScript中只有6个假值: undefined null NaN 0 '' (empty string) false 函数构造函数,如new Number和new Boolean都是真值。
    (() => {
      let x, y;
      try {
        throw new Error();
      } catch (x) {
        (x = 1), (y = 2);
        console.log(x);
      }
      console.log(x);
      console.log(y);
    })();
    
    A:
    1 undefined 2 B: undefined undefined undefined C: 1 1 2 D: 1 undefined undefined 答案: A catch块接收参数x。当我们传递参数时,这与变量的x不同。这个变量x是属于catch作用域的。 之后,我们将这个块级作用域的变量设置为1,并设置变量y的值。 现在,我们打印块级作用域的变量x,它等于1。 在catch块之外,x仍然是undefined,而y是2。 当我们想在catch块之外的console.log(x)时,它返回undefined,而y返回2。
    setInterval(() => console.log("Hi"), 1000);
    
    A:一个唯一的id
    B:指定的毫秒数
    C:传递的函数
    D:undefined
    
    答案: A
    它返回一个唯一的id。 此id可用于使用clearInterval()函数清除该定时器。
    

     from:https://juejin.im/post/5d0644976fb9a07ed064b0ca

    https://juejin.im/post/5bf769e0518825773a2ebfe5#comment

    实现一个get函数,使得下面的调用可以输出正确的结果
    const obj = { selector: { to: { toutiao: "FE Coder"} }, target: [1, 2, { name: 'byted'}]};
    
    get(obj, 'selector.to.toutiao', 'target[0]', 'target[2].name');
    // [ 'FE Coder', 1, 'byted']
    function get(data, ...args) {
        const res = JSON.stringify(data);
        return args.map((item) => (new Function(`try {return ${res}.${item} } catch(e) {}`))());
    }
    
    const obj = { selector: { to: { toutiao: "FE Coder"} }, target: [1, 2, { name: 'byted'}]};
    
    console.log(get(obj, 'selector.to.toutiao', 'target[0]', 'target[2].name', 'asd'));
    有人提到了那种Function的方式没办法处理以下的处理:
    let obj = {time : new Date(), a : "this is a", b : 30};
    
    因为JSON.stringfy后,Date、Function和RegExp类型的变量都会失效。对于这种情况,评论区有个大佬(冯恒智)也提到了一种很好的解决方案:
    function get(data, ...args) {
        return args.map((item) => (new Function('data',`try {return data.${item} } catch(e) {}`))(data));
    }
    1、数组的索引和对象key有什么关系?
    数组是对象的特殊形式,使用方括号访问数组元素和使用方括号访问对象属性一样。JavaScript将指定的数字索引值转换成字符串——索引1变成"1"——然后将其作为属性名来使用。数组的特别之处在于,当使用小于2^32的非负整数作为属性名时数组会自动维护其length属性。
    // 索引到属性名的转化
    let arr = [1,2,3];
    console.log(arr[1]) // 2
    console.log(arr["1"]) // 2
    
    所有的数组都是对象,可以为其创建任意名字的属性,不过,只有在小于2^32的非负整数才是索引,数组才会根据需要更新length。事实上数组的索引仅仅是对象属性名的一种特殊类型,这意味着JavaScript数组没有“越界”错误的概念。当查询任何对象中不存在的属性时,不会报错,只会得到undefined let arr = []; arr["a"] = 1; console.log(arr,arr.length) // arr是[a:1] length是0
    对于使用负数或非整数的情况,数值会转换为字符串,字符串作为属性名来用,当时只能当做常规的对象属性,而非数组的索引。 let arr = []; arr[-1.23] = 0; console.log(arr,arr.length) // arr是[-1.23: 0] length是0

    使用非负整数的字符串或者一个跟整数相等的浮点数时,它就当做数组的索引而非对象属性。 let arr = []; arr["100"] = 'a'; console.log(arr,arr.length) // arr 是[empty × 100, "a"],length 是101 let arr1 = []; arr1[1.0000] = 'b'; console.log(arr1,arr1.length) // arr 是[empty, "b"],length 是2

    from:https://juejin.im/post/5b684ef9e51d451964629ba1

    数组的性能提升:http://www.wemlion.com/post/javascript-array-evolution-performance/

  • 相关阅读:
    Jmeter之http性能测试实战 非GUI模式压测 NON-GUI模式 结果解析TPS——干货(十一)
    UI Recorder 自动化测试 回归原理(九)
    UI Recorder 自动化测试 录制原理(八)
    UI Recorder 自动化测试 整体架构(七)
    UI Recorder 自动化测试 配置项(六)
    UI Recorder 自动化测试 工具栏使用(五)
    UI Recorder 自动化测试 回归测试(四)
    UI Recorder 自动化测试 录制(三)
    UI Recorder 自动化测试工具安装问题疑难杂症解决(二)
    UI Recorder 自动化测试安装教程(一)
  • 原文地址:https://www.cnblogs.com/little-ab/p/11084355.html
Copyright © 2011-2022 走看看