Objects, Functions, Classes - 25% Flashcards
Create an obj
Object literal:
const obj = { a: “b” };
Object constructor:
const obj = new Object( )
delete operator
Use to delete a key/value pair from an obj:
delete myObj.key;
keys w/space
use quotation
ex: { “my key”: 2 }
existence of a property in an obj
Can use “in” operator to test for existence of a property:
“nonExist” in myObj // false
Reading a non-existent property returns undefined (not null):
myObj.nonExist === undefined
Obj Iteration
for (key in object) { }
Object property order
Properties that have keys that can be converted to Integers are sorted, the rest are in creation order
Object Inheritance
All objects inherit from at least 1 other obj
The object being inherited from is called the prototype,
and the inherited properties can be found in the prototype object of the constructor
You can add a property to all objects created through a certain constructor using the prototype property:
Car.prototype.color = ‘red’;
“this” for obj
Can use w/in a method to refer to the current object.
In a Class, cannot access “this” from w/in a nested fn, unless using arrow fn nested once (skips closest encompassing layer)
getters and setters
Within object initializers, they are prefixed with the keywords get or set.
When the getter is called, a fn is run and a value is returned.
When a setter is called, a parameter is accepted and a fn runs
const myObj = {
a: 7,
get b() {
return this.a + 1;
},
set c(x) {
this.a = x / 2;
},
};
console.log(myObj.a); // 7
console.log(myObj.b); // 8
myObj.c = 50; // Calls the set c(x) method
console.log(myObj.a); // 25
Comparing objs
Can’t be done with equality operator - 2 objs aren’t equal even if same keys/values.
For simple objs, can use JSON.stringify for comparison, but if key/vals aren’t in same order, it won’t work.
JS Class
A type of fn used to create mult objs w/the same properties and methods.
Also used to implement OOP concepts in JS, such as inheritance, encapsulation, and polymorphism.
It was introduced in (ES6) as a new syntax over the existing prototype-based inheritance.
They provide a more intuitive and clear syntax to create objects and deal with inheritance.
constructor
Creates a new object,
Binds ‘this’ to the new object,
Runs the code in the constructor,
Returns the new object.
Ex:
class Person {
constructor(name) {
this.name = name;
}
}
const gal = new Person(“Court”);
^ this calls the constructor fn
declaring it is optional, all will have a constructor fn built in.
Class Inheritance
Use “extends” keyword to declare a subclass, and inherit the properties and methods of the parent class.
Uses super( ) in the constructor method to pass the parent properties up, then can add its unique initialization.
Encapsulation
If a property or fn is declared using #, it is private and can only be reached from inside that Class.
Modules
Ways of splitting JS programs into separate units that can be imported when needed. Node.js has this ability and some JS libraries / frameworks use them.
Module usage
The <script type="module">
attribute is used to denote when a module is being pointed to.
Can export props and fns by adding to end of file:
export { name, fn1, fn2 };
or exporting each individually. Can’t use * w/export.
Use import statement - import { name, fn1, fn2 } from “./modules/square.js”;
The path - starting w/dot means starting at the current location.
Can use “as” in the import to change their reference keyword.
Types of Scope
Global scope: The default scope for all code running in script mode.
Module scope: The scope for code running in module mode.
Function scope: The scope created with a function.
Block scope: The scope created with a pair of curly braces (a block) - this is for let and const, not var
Execution flow
Top-down.
JS first checks for function and variable declarations and then executes the code. This concept is known as hoisting.
Ex - var
console.log(myVar); // This will output: undefined
var myVar = 5;
console.log(myVar); // This will output: 5
Ex - let (const is the same)
console.log(myLet); // ReferenceError: myLet is not defined
let myLet = 5;
console.log(myLet); // 5
continue (in a loop)
The continue statement skips an iteration if a certain condition returns true.
decorators
A wrapper fn that wraps an existing fn and extends its behavior Can be used to extend bx of objects without affecting other objects of the same class.
When it is a Class decorator, should receive only 1 arg.
closure fn
A fn that has access to the parent scope, even after the parent function has closed. This means that a fn defined in the scope of another fn will continue to have access to variables in its parent fn and global scope, even after the parent fn has finished executing.
privileged fn
A public fn that has access to private fns or variables. In other words, it is a method which is accessible in the public scope but has access to private data or methods.
Privileged methods are usually created by declaring them with the ‘this’ keyword inside the constructor.
property descriptors
writable - boolean, can use to make readonly.
An obj that allows you to define or modify the attributes of existing properties on an obj. Attributes:
- Value: The value associated with the property. Can be any valid JavaScript value (number, object, function, etc).
- Writable: A Boolean indicating whether the value of the property can be changed.
- Enumerable: A Boolean indicating whether the property can be enumerated.
- Configurable: A Boolean indicating whether the property can be changed and deleted from the object.
- Get: A function which serves as a getter for the property, or undefined if there is no getter. The function’s return value will be used as the value of the property.
- Set: A function which serves as a setter for the property, or undefined if there is no setter. The function will receive as its only argument the new value being assigned to the property.
These descriptors can be accessed using methods like Object.getOwnPropertyDescriptor() and can be defined or modified using methods like Object.defineProperty() and Object.defineProperties().
Object.entries
Returns an array containing all of the [key, value] pairs of a given object’s own enumerable string properties.
Object.fromEntries() - reverse of Object.entries:
Returns a new object from an iterable of [key, value] pairs.