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/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM