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.