Object Oriented Programming Module #19 Flashcards
What is this syntax: const car = {
}
A JavaScript Object. This particular syntax is aka object literal syntax. It starts with a variable, then the = assignment operator, follow by open and closing curly braces.
Define a JavaScript object.
Anything that is not a primitive. Functions, arrays, or object literals.
There are 3 ways to create an object in JS. We already know the “object literal” syntax, what’s the second way to create an object?
The Object.create( ) method creates a new object, using an existing object as the prototype of the newly created object.
const car = Object.create( ) Declare a variable then assign = name.create( )
What is the 3rd way to create and “initialize” an object in JavaScript?
By using a constructor.
The constructor method is a special method of a class for creating and initializing an object of that class.
const myCar = new Car("Ford", "Fiesta") myCar.brand //'Ford' myCar.model //'Fiesta'
Example from MDN:
class Polygon { constructor( ) { this.name = 'Polygon'; } } const poly1 = new Polygon( ); console.log(poly1.name); // expected output: "Polygon"
What’s one way to access an object’s property?
Dot notation. car.color //’blue’
What is returned when accessing a property that doesn’t exist?
//undefined
What’s the shorthand for deleting a property from an object?
delete card.model
Aside from dot notation, what’s another way to delete a property from an object?
Brackets. For example:
delete car [ ‘color’ ]
How can you invoke a function that has been assigned to an object?
By using dot notation syntax exactly like a method. For example:
const car = { brand: "Ford", model: "Fiesta", start: function () { console.log("Started") }, }
car.start()
Inside a method defined using a function( ) { } syntax we have access to the object instance by referencing this.
How are arrow functions bound to objects?
They aren’t you can attach an arrow function to a property in an object but you won’t be able to access it.
How can you add a parameter to a method?
The same way in which you’d add a param to to a function:
car.goTo(“Rome”)
How is a class written?
Using the “class” keyword, an identifier starting with a capital letter “Name”, a method, a parameter and then the body. Example:
class Person { hello( ) { return 'Hello, I am Flavio' } }
What is the purpose of a class in JavaScript?
They are a way to define a common pattern for multiple objects.
How do you write a method inside of a class?
Almost exactly like a function, except without the function key word.
Example:
class Person { hello( ) { return 'Hello, I am Flavio' } }
We can also invoke methods on an instance of the class:
class Person { hello( ) { return 'Hello, I am Flavio' } } const flavio = new Person( ) flavio.hello( )
How can you initialize class properties when we create a new object instance.
By using the constructor method, for example:
class Person { constructor(name) { this.name = name }
hello( ) {
return ‘Hello, I am ‘ + this.name + ‘.’
}
}