Are 'length' and '__proto__' made of 'getters' and 'setters'?

Viewed 73

When I first encountered the basics of JavaScript, the most curious thing was why 'length' and 'proto' works different from any other properties.

I could get a clue when I heard that there were 'getters' and 'setters' in JS.

However, I am asking this question becaues I still have questions after I study more.

[1] for 'proto'

let obj = {}
obj.hasOwnProperty('__proto__') // false
Object.getOwnPropertyDescriptor(Object.prototype, '__proto__')
/*
{
  configurable: true,
  enumerable: false,
  get: [[some function]],
  set: [[some function]]
}
*/

I've found that 'proto' is an accessor property in Object.prototype, not a data property in obj.

[2] for 'length'

let arr = [1, 2]
Object.getOwnPropertyDescriptor(arr, 'length')
/*
{
  value: 2,
  writable: true,
  enumerable: false,
  configurable: false
}
*/
Object.getOwnPropertyDescriptor(Array.prototype, 'length')
/*
{
  value: 0,
  writable: true,
  enumerable: false,
  configurable: false
}
*/

The case of 'length' is different. It's data property of arr. (Array.prototype also have one)

I'm curious how length properties are renewed whenever arrays change, or they remove/add elements (even if it is undefined) whenever 'length' changes.

Is it just the grammatical action of JavaScript??

I could say this differently.

Can I assign properties which act like 'length' to my own objects??

0 Answers
Related