Skip to main content

Command Palette

Search for a command to run...

CTN 09. x++ và ++x, bind method (Function constructor) và Object.defineProperties, Object.getOwnPropertyNames

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

var ronaldo = { age: 34 };

var messi = { age: 32 };

function score(year, tr, t) {
  if (typeof tr === "function" && typeof t === "function") {
    console.log(`You score ${tr(year, t(this.age))} times`);
  }
}

const transform = (x, y) => x - y;

const title = (x) => ++x + x++;

const helloRonaldo = score.bind(ronaldo, 2029, transform, title);

helloRonaldo();

const helloMessi = score.bind(messi, 2029, transform, title);

helloMessi();

Lời giải:

var ronaldo = { age: 34 };

var messi = { age: 32 };

function score(year, tr, t) {
  if (typeof tr === "function" && typeof t === "function") {
    console.log(`You score ${tr(year, t(this.age))} times`);
  }
}

const transform = (x, y) => x - y;

const title = (x) => ++x + x++;
// giả sử: x = 1
// biểu thức 1: ++x <=> trả về giá trị x + 1  x = x + 1 => result = 2
// biểu thức 2: x++ <=> trả về giá trị x  x = x + 1 => result = 2 (do x = 2 sau khi biểu thức 1 được thực thi)
// biểu thức 3: ++x + x++ = 2 + 2 = 4

const helloRonaldo = score.bind(ronaldo, 2029, transform, title);

helloRonaldo();
// t(this.age) = t(34) = 35 + 35 = 70
// tr(year, t(this.age)) = year - 70 = 2029 - 70 = 1959
// => `You score 1959 times`

const helloMessi = score.bind(messi, 2029, transform, title);

helloMessi();
// t(this.age) = t(32) = 33 + 33 = 66
// tr(year, t(this.age)) = year - 66 = 2029 - 66 = 1963
// => `You score 1963 times

Kiến thức liên quan:

  • x++ và ++x
  • Function contructor method: bind

Bài 2

var person = {};

Object.defineProperties(person, {
  name: {
    value: "Vuong",
    enumerable: true,
  },
  job: {
    value: "developer",
    enumerable: true,
  },
  studying: {
    value: "PhD",
    enumerable: true,
  },
  money: {
    value: "NZD",
    enumerable: false,
  },
});

class Evaluate {
  static checkFlag(obj) {
    return Object.getOwnPropertyNames(obj) > Object.keys(obj)
      ? Object.getOwnPropertyNames(obj)
      : Object.keys(obj);
  }
}

const flag = Evaluate.checkFlag(person);

console.log(flag.length);

Lời giải:

var person = {};

Object.defineProperties(person, {
  name: {
    value: "Vuong",
    enumerable: true,
  },
  job: {
    value: "developer",
    enumerable: true,
  },
  studying: {
    value: "PhD",
    enumerable: true, // Mặc định khi tạo object thì thuộc tính enumerable = true
  },
  money: {
    value: "NZD",
    enumerable: false, // Object.keys sẽ không lấy ra được key này (money)
  },
});

class Evaluate {
  static checkFlag(obj) {
    return Object.getOwnPropertyNames(obj) > Object.keys(obj) 
      // Object.getOwnPropertyNames(obj) = ['name', 'job', 'stydying', 'money']
      // Object.keys(obj) = ['name', 'job', 'stydying'], do money được defined với thuộc tính "enumerable: false"
      // Để thực hiện phép so sánh trong JS, dữ liệu sẽ được convert sang dạng string hoặc number trước khi so sánh
      // => Object.getOwnPropertyNames(obj) > Object.keys(obj) = true
      ? Object.getOwnPropertyNames(obj)
      : Object.keys(obj);
  }
}

const flag = Evaluate.checkFlag(person);
// Có thể gọi trực tiếp Evaluate.checkFlag bởi vì checkFlag là static function
// => kết quả = ['name', 'job', 'stydying', 'money']

console.log(flag.length); // = 4

Kiến thức liên quan:

  • Object.defineProperties
  • Object.getOwnPropertyNames
8 views

More from this blog

zujs

35 posts