Skip to main content

Command Palette

Search for a command to run...

CTN 12. Object.entries, Object.values & Truthy, Falsy value, bind this

Published
3 min read

Hello! I'm Zu.Doan

Series này mình muốn rèn luyện khả năng suy nghĩ và giải bài tập, qua đó để hiểu rõ hơn các khái niệm của Javascript.

Bài 1

class MySort {
  constructor(object) {
    this.object = object;
  }

  getSort() {
    return Object.entries(this.object)[0][1].sort()[
      Object.values(this.object).length
    ];
  }
}

const object = {
  month: ["July", "September", "January", "December"],
};

const sortMe = new MySort(object);

console.log(sortMe.getSort());

Lời giải:

class MySort {
  constructor(object) {
    this.object = object;
  }

  getSort() {
    return Object.entries(this.object)[0][1].sort()[
      // Object.entries(object) sẽ trả về một mảng chứa các mảng con 
      // (mà mỗi mảng con đó chứa 2 phần tử [key of phần tử x, value of phần tử x])
      // với x lần lượt từ phần tử đầu tiên tới phần tử cuối cùng của object
      // => Object.entries(this.object) = [['month', ["July", "September", "January", "December"]]]
      // => Object.entries(this.object)[0][1] = ["July", "September", "January", "December"]
      // => sort() = ["December", "January", "July", "September"]

      Object.values(this.object).length
      // Object.values(object) trả về một mảng các values trong object
      // => [["July", "September", "January", "December"]]
      // => length = 1
    ];
  }
}

const object = {
  month: ["July", "September", "January", "December"],
};

const sortMe = new MySort(object);

console.log(sortMe.getSort());
// sortMe.getSort() = ["December", "January", "July", "September"][1] = "January"

Kiến thức liên quan:

  • Object.entries
  • Object.values

Bài 2

const flag = [] !== !!!!![];

let f = () => {};

// console.log((typeof f()).length + flag.toString().length);

Lời giải

const flag = [] !== !!!!![];
// [] là truthy => !!!!![] = false
// [] !== false => flag = true

let f = () => {};

// console.log((typeof f()).length + flag.toString().length);
// Chú ý là đang lấy typeof của giá trị trả về khi thực thi function f
// chứ không phải là (typeof f)
// => (typeof f()) = undefined // do hàm f không trả về giá trị nào
// 'undefined'.length + 'true'.length = 9 + 4 = 12

Kiến thức liên quan:

  • Falsy value: false / 0 / -0 / 0n (BigInt) / '' "" `` (empty string) / NaN / null / undefined / document.all (in browser)
  • Truthy value: là những giá trị khác với Falsy value
  • type of variable

Bài 3

(function (a, b, c) {
  arguments[2] = (typeof arguments).length;

  c > 10 ? console.log(c) : console.log(++c);
})(1, 2, 3);

Lời giải:

(function (a, b, c) {
  // arguments = [a, b, c] = [1, 2, 3]
  arguments[2] = (typeof arguments).length;
  // (typeof arguments) = 'object'.length = 6
  // => arguments = [1, 2, 6]

  // c = arguments[2] = 6
  // => console.log(++6) => in ra 7
  c > 10 ? console.log(c) : console.log(++c);
})(1, 2, 3);

Kiến thức liên quan:

  • arguments in function

Bài 4

var name = "Auckland";

const nz = {
  name: "Kiwi",

  callMe: function () {
    return this.name;
  },
};

let me = nz.callMe;

let she = nz.callMe.bind(nz);

let result = me() === nz.callMe() ? she() : `${me()} ${she()}`;

console.log(result);

Lời giải

var name = "Auckland";

const nz = {
  name: "Kiwi",

  callMe: function () {
    return this.name;
  },
};

let me = nz.callMe;
// khi gọi me() thì this trong hàm callMe sẽ trỏ đến biến toàn cục global
// => this.name = "Auckland"

let she = nz.callMe.bind(nz);
// khi gọi she() tương tự như me() this sẽ trỏ tới biến global
// tuy nhiên ta lại sử dụng bind(nz) => this trỏ tới nz object
// => this.name = "Kiwi"

let result = me() === nz.callMe() ? she() : `${me()} ${she()}`;
// me() === nz.callMe() => false
// `${me()} ${she()}` = "Auckland Kiwi"

console.log(result); // "Auckland Kiwi"

Kiến thức liên quan:

  • this
  • bind (bind this)
42 views