Object.getOwnPropertyDescriptor
The method Object.getOwnPropertyDescriptor allows to query the full information about a property.
The syntax is:
let descriptor = Object.getOwnPropertyDescriptor(obj, propertyName);
obj
The object to get information from.
propertyName
The name of the property.
The returned value is a so-called “property descriptor” object: it contains the value and all the flags.
For instance:
let user = {
name: "John"
};let descriptor = Object.getOwnPropertyDescriptor(user, ‘name’);
alert( JSON.stringify(descriptor, null, 2 ) );
/* property descriptor:
{
"value": "John",
"writable": true,
"enumerable": true,
"configurable": true
}
*/
put if if you query the prototype property descriptors you'll see enumerable as false. That is why in for(let key in obj) instance you don't see prototype properties, only instance properties.Object.defineProperty(user, ‘name’) allows to override property descriptors.
Link: https://www.udemy.com/course/javascript-object-oriented-programming/learn/lecture/10019954#questions/12951222/
Object.getPrototypeOf()
The Object.getPrototypeOf() method returns the prototype (i.e. the value of the internal [[Prototype]] property) of the specified object.
prototype chain
what can you console.log
property attributes
js is a prototype based language
object’s prototype vs prototype property on constructor functions
JavaScript is often described as a prototype-based language — to provide inheritance, objects can have a prototype object, which acts as a template object that it inherits methods and properties from.
An object’s prototype object may also have a prototype object, which it inherits methods and properties from, and so on. This is often referred to as a prototype chain, and explains why different objects have properties and methods defined on other objects available to them.
In JavaScript, a link is made between the object instance and its prototype (its __proto__ property, which is derived from the prototype property on the constructor), and the properties and methods are found by walking up the chain of prototypes.
Note: It’s important to understand that there is a distinction between an object’s prototype (available via Object.getPrototypeOf(obj), or via the deprecated __proto__ property) and the prototype property on constructor functions.
The former is the property on each instance, and the latter is the property on the constructor. That is, Object.getPrototypeOf(new Foobar()) refers to the same object as Foobar.prototype.
The constructor property in a prototype is automatically setup to reference the constructor function.
see prototype chain illustration
Links: https://www.udemy.com/course/javascript-object-oriented-programming/learn/lecture/10019938#questions/12951222/
https://stackoverflow.com/questions/572897/how-does-javascript-prototype-work
prototype chain illustration
https: //i.stack.imgur.com/FPPdI.png
https: //i.stack.imgur.com/m5DXc.png
Because class is syntactic sugar over constructor function Person class inherits from Function Prototype Also from Person.prototype new Person constructor creates an instance of person and inherits also from Person.Prototype Function.prototype inherits from Object.Prototype Thief class prototype is new Person() constructor
prototype trivia
* parent methods might be good on most child objects, but not some, so what to do with these exceptions?
benefits of oop
composition over inheritance
rest vs spread operator
function mixin(target, ... sources){
Object.assign(target, ...sources)
}prototypical inheritance example with circle and shape super method overriding call the base implementation levels of inheritance mixins
function Shape() {}
function Circle() {}
// Prototypical inheritance Circle.prototype = Object.create(Shape.prototype); Circle.prototype.constructor = Circle;
function Rectangle(color) {
// To call the super constructor
Shape.call(this, color);
}
// Method overriding
Shape.prototype.draw = function() {}
Circle.prototype.draw = function() {
// Call the base implementation Shape.prototype.draw.call(this);
// Do additional stuff here }
// Don't create large inheritance hierarchies. // One level of inheritance is fine.
// Use mixins to combine multiple objects
// and implement composition in JavaScript.
const canEat = {
eat: function() {}
};
const canWalk = {
walk: function() {}
};
function mixin(target, …sources) {
// Copies all the properties from all the source objects // to the target object. Object.assign(target, ...sources); }
function Person() {}
mixin(Person.prototype, canEat, canWalk);
classes in es6 instance methods typeOf a class factory vs constructor class declaration instead of expression? instance and static methods
source file
A source program is a text file that contains instructions written in a high level language. It can not be executed (made to run) by a processor without some additional steps. A source program is also called a source file, source code, or sometimes, just source.
compiler vs transpiler
interpreter and traslator
Compiler - compiles code to a lower level code.
Translator converts the source code from one programming language to another programming language of the same or different level of abstraction.
By definition transpiler is a special form of translator.
Transpiler - compiles code to same level of code/abstraction.
Example:
“Developer code” -> “Another developer code or version”
JavaScript ES2015+ -> JavaScript ES5
synonim parent class vs child class
base/super/parent vs derived/child/sub
property descriptor
A property descriptor encodes the attributes of a property as a JavaScript object. Their TypeScript interfaces look as follows.
interface DataPropertyDescriptor {
value?: any;
writable?: boolean;
configurable?: boolean;
enumerable?: boolean;
}
interface AccessorPropertyDescriptor {
get?: (this: any) => any;
set?: (this: any, v: any) => void;
configurable?: boolean;
enumerable?: boolean;
}
type PropertyDescriptor = DataPropertyDescriptor | AccessorPropertyDescriptor;Property attributes
There are two kinds of properties and they are characterized by their attributes:
A data property stores data. Its attribute value holds any JavaScript value.
An accessor property consists of a getter function and/or a setter function. The former is stored in the attribute get, the latter in the attribute set.
Additionally, there are attributes that both kinds of properties have. The following table lists all attributes and their default values.
Kind of property
Name and type of attribute
Default value
Data
property value: any undefined
writable: boolean false
Accessor property
get: (this: any) => any undefined
set: (this: any, v: any) => void undefined
All properties
configurable: boolean false
enumerable: boolean false