๐ธprototype
function's `prototype` property.
Last updated
Was this helpful?
function's `prototype` property.
Last updated
Was this helpful?
Was this helpful?
JS โฉ value โฉ object โฉ function โฉ prototype
only a relatively small number of objects have a "prototype" property, it's these objects that define the prototypes for all the other objects.
let obj = new F(); // obj.__proto__ === F.prototype
โ๏ธ function's prototype vs. class's prototype (property)
function's prototype is writable. (can be reassigned)
's prototype is read-onlyโ(can't be reassigned)
function's prototype (property)
is (only) used as a new object's .
โญ๏ธ ๆณจๆ๏ผ
ๅฆๆๆนๅฏซๅฝๆธ็ prototype๏ผๅฆ๏ผA.prototype = {}
๏ผๅๅพๅพ็ new A() ็ฉไปถๅฐฑๅไนๆฒๆ constructor ๅฑฌๆง๏ผ้ค้ไฝ ่ชๅทฑๅจๆฐ็ prototype ไธญๅๅ ๅ
ฅ constructor ๅฑฌๆงโ๏ธ(่็็ฉไปถๆฒๆๅฝฑ้ฟ๏ผๅ ็บ่็ prototype ไพ็ถๅญๅจ๏ผ่็็ฉไปถ็ [[Prototype]]
ๅฑฌๆงๆฒๆๆน่ฎ๏ผไพ็ถๆๅ่็ prototype)
should be either an object or null, other values wonโt workโ
๐ javascript.info
function A(){}
A.prototype.constructor === A // true
function test_SheetPrototype() {
const sheet = app.sheet.active; // custom getter: returns `Sheeet`
let obj = sheet;
while (obj) {
const keys = Reflect.ownKeys(obj);
const n = keys.length;
const k = 3; // # of sliced items
log(
`object: ${obj}` +
`, type: ${typeName(obj)}` +
`, keys: (${n} items) [${keys.slice(0, k)}${n > k ? ' ...' : ''}]`
);
log(keys); // show all own keys (enum + non-enum + symbol)
obj = obj.__proto__; // === Object.getPrototypeOf(obj)
}
}
object type keys
------------------------------------------------------------------
Sheet object (141 items) [toString,getFilter,clear ...]
[Object]* object ( 12 items) [constructor,__defineGetter__,__defineSetter__ ...]
------------------------------------------------------------------
* last object in the prototype chain === Object.prototype โญ๏ธ