Advanced Objects & Methods
Definition
Create an object
Just like a JSON object ha...
Constructor Function
What if I want to add another book?
Use constructor function to create new objects
Setting up Prototype Object
Shared Methods (functions) between Instance Objects
Remeber to use this and DO NOT USE arrow function
Please note an important use case regarding variable scope
Prototypal Inheritance
The instance look for property and methods inside itself first, before looking up to the prototype.
Primitives & Objects
There are many default methods and functions related to objects
WHY WE CAN USE A METHOD BUT IT DOES NOT EXIST?
Because...
'hasOwnProperty' mean a property at object level, not higher level
You can even create a new default Object method, and use everywhere
Different synctax for new objects
Primitives for special Objects
myArray — Array.Prototype — Object.prototype — null
myFunction — Function.prototype — Object.prototype — null
myNumber — Number.prototype — Object.prototype — null
myString — String.protoype — Object.prototype — null
Class Syntax
class = constructor function + prototype methods
IMPORTANT: no comma , between contructor and method names in class
Subclass
Person → Employee, Student
Book → Paper Book, Podcast Book
Getter & Setter
Get correct data type with validation
Use getter and setter in class
THIS IS USEFUL TO REPLACE AUTOMATIC METHODS WITH PROPERTY
De-structuring
Closures
Run a function to create (return) a function
This is useful to manipulate objects
Advanced Synctax
Shorthand Synctax for Object Properties
If object properties are the same as variables, you can use:
Destructuring Synctax for Object Properties
Convert object properties into variables quickly, using the same property names:
Applications for Destructuring
