( O O P ) OBJECT ORIENTED PROGRAMMING. Flashcards
What are JS OBJECTS?
1.) A JavaScript object is an entity that has
properties.
2.) Each property is a key/value pair.
3.) The key is the property name.
The value of a property can be a number, string, etc or a function. In this case, the property is called a method.
How do you create Objects?
An object can be created with figure brackets {…} with an optional list of properties. A property is a “key: value” pair, where key is a string (also called a “property name”), and value can be anything.
Adding and removing from an Object
We can add, remove and read files from it at any time.
1.) user.isAdmin = true; uses boolean to add
2.) delete user.age; uses delete
3.) We can also use multiword property names, but then they must be quoted
{ “likes birds”: true}
Property names limitations
A variable cannot have a name equal to one of the language-reserved words like
“for”,
“let”,
“return” etc.
But for an object property, there’s no such restriction
Object Magic
it’s possible to access any property. There will be no error if the property doesn’t exist!
Reading a non-existing property just returns undefined. So we can easily test whether the property exists
What is Object reference?
Objects are assigned and copied by reference. In other words, a variable stores not the “object value”, but a “reference” (address in memory) for the value. So copying such a variable or passing it as a function argument copies that reference, not the object itself.
How do you clone and merge Objects?
To make a “real copy” (a clone) we can use
1.) Object.assign for the so-called “shallow copy” (nested objects are copied by reference)
2.) A “deep cloning” function structuredClone or
3.)use a custom cloning implementation, such as _.cloneDeep(obj).
What is JS Garbage Collection?
The main things to know:
1.) Garbage collection is performed automatically. We cannot force or prevent it. 2.) Objects are retained in memory while they are reachable. 3.) Being referenced is not the same as being reachable (from a root): a pack of interlinked objects can become unreachable as a whole,
How do functions work in Objects?
1.) Functions that are stored in object properties are called “methods”.
2.) Methods allow objects to “act” like object.doSomething().
3.) Methods can reference the object as this.
How does “this” apply in methods?
1.The value of this is defined at run-time.
2.)When a function is declared, it may use this, but that this has no value until the function is called.
3.) A function can be copied between objects.
4.) When a function is called in the “method” syntax:
object.method(), the value of this during the call is object.
What is Optional-Chaining?
The optional chaining ?. syntax has three forms:
1.) obj?.prop – returns obj.prop if obj exists, otherwise undefined.
2.) obj?.[prop] – returns obj[prop] if obj exists, otherwise undefined.
3.) obj.method?.() – calls obj.method() if obj.method exists, otherwise returns undefined.
A chain of ?. allows to safely access nested properties.
Still, we should apply ?. carefully, only where it’s acceptable, according to our code logic, that the left part doesn’t exist. So that it won’t hide programming errors from us, if they occur.
What is a Symbols() in JS ?
Symbol is a primitive type for unique identifiers.
1.) Symbols are created with Symbol() call with an optional description (name).
2.) Symbols are always different values, even if they have the same name. If we want same-named symbols to be equal, then we should use the global registry: Symbol.for(key) returns (creates if needed) a global symbol with key as the name. Multiple calls of Symbol.for with the same key return exactly the same symbol.
What are the two main use cases for Symbols() ?
“Hidden” object properties.
1.) If we want to add a property into an object that “belongs” to another script or a library, we can create a symbol and use it as a property key. A symbolic property does not appear in for..in, so it won’t be accidentally processed together with other properties. Also it won’t be accessed directly, because another script does not have our symbol. So the property will be protected from accidental use or overwrite.
So we can “covertly” hide something into objects that we need, but others should not see, using symbolic properties. 2.) There are many system symbols used by JavaScript which are accessible as Symbol.*. We can use them to alter some built-in behaviors. For instance, later in the tutorial we’ll use Symbol.iterator for iterables, Symbol.toPrimitive to setup object-to-primitive conversion and so on.
What is Object Oriented Programming?
1.) Object-Oriented Programming, or OOP, is a programming paradigm that uses objects containing both data and behavior to create programs.
2.) A class is an object-oriented abstraction for an idea or a concept manipulated by a program. It offers a convenient syntax to create objects representing this concept.
3.) A JavaScript class is defined with the class keyword. It can only contain methods. The constructor() method, called during object creation, is used to initialize the object, often by giving it some data properties. Inside methods, the this keyword represents the object on which the method was called.
What are OOP Prototypes?
1.) JavaScript’s OOP model is based on prototypes. Any JavaScript object has an internal property which is a link (a reference) to another object: its prototype. Prototypes are used to share properties and delegate behavior between objects.
2.) When trying to access a property that does not exist in an object, JavaScript tries to find this property in the prototype chain of this object by first searching its prototype, then its prototype's own prototype, and so on. 3.) There are several ways to create and link JavaScript objects through prototypes. One is to use the Object.create() method.