OOP (Object Oriented Programming) Flashcards
What is a method?
A function stored in a property of an object
How can you tell the difference between a method definition and a method call?
o Method definitions will always have a function keyword and a code block
o Method calls start with an object name and has arguments that are being passed
Describe method definition syntax (structure).
propertyName: functionName( ) {
Return ‘anything’;
}
Describe method call syntax (structure).
objectName.method( )
How is a method different from any other function?
They are functions stored in an object
What is the defining characteristic of Object-Oriented Programming?
Objects can contain both:
- Data (as properties) - Behavior (as methods)
What are the four “principles” of Object-Oriented Programming?
o Abstraction
o Encapsulation
o Inheritance
o Polymorphism
What is “abstraction”?
Being able to work with (possibly) complex things in simple ways (such as, turning a light on/off)
What does API stand for?
Application Programming Interface
What is the purpose of an API?
Give programmers a way to interact with a system(s) in a simplified, consistent fashion (aka abstraction) without knowing about how they all work independenly.
What is ‘this’ in JavaScript?
The object that you’re working within
What does it mean to say that this is an “implicit parameter”?
The parameter is available (in a function’s code block) even though it was never included in the function’s parameter list OR declared in a var
When is the value of ‘this’ determined in a function; call time or definition time?
Call time
What does ‘this’ refer to in the following code snippet?
var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
Undefined (there is nothing in front of greet)
Given this character object:
var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
What is the result of the following code snippet? Why?
character.greet();
It’s a me, Mario! Because greet is being called on the character object
Given this character object:
var character = { firstName: 'Mario', greet: function () { var message = 'It\'s-a-me, ' + this.firstName + '!'; console.log(message); } };
What is the result of the following code snippet? Why?
var hello = character.greet;
hello();
It’s a me, undefined!
Because ‘this’ is being called from the window object and not the character object
How can you tell what the value of ‘this’ will be for a particular function or method DEFINITION?
There is no way to know in a definition because there is no value yet
How can you tell what the value of this is for a particular function or method CALL?
By checking the object left of the dot
What kind of inheritance does the JavaScript programming language use?
Prototype-based (prototypal) inheritance
What is a prototype in JavaScript?
Object with properties and methods that can be reused
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on strings, arrays, and numbers?
Via inheritance from the prototypes (prototypal inheritance)
If an object does not have its own property or method by a given key, where does JavaScript look for it?
its prototype (starts from the nearest prototype and moves up until it finds what it needs)
What does the ‘new’ operator do?
Let you create an instance of a user-defined or built-in object type that has a constructor function (create a function that generates objects for you).
- Creates a blank, plain JavaScript object.
- Adds a property to the new object (__proto__) that links to the constructor function’s prototype object
- Binds the newly created object instance as the this context (i.e. all references to this in the constructor function now refer to the object created in the first step).
- Returns this if the function doesn’t return an object.
What property of JavaScript functions can store shared behavior for instances created with new?
prototype property
What does the instanceof operator do?
Tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object and returns a Boolean value
What is a “callback” function?
A function passed into another function as an argument
Besides adding an event listener callback function to an element or the document, what is one way to delay the execution of a JavaScript function until some point in the future?
setTimeOut()
How can you set up a function to be called repeatedly without using a loop?
setInterval()
What is the default time delay if you omit the delay parameter from setTimeout() or setInterval()?
0 (it will execute as soon as all the other codes finish running)
What do setTimeout() and setInterval() return?
timeoutID (you always want to set your var countdownId = setInterval( variable, time) so when you declare your clearInterval(), you can pass your countdownId so it stops the process when you need it to instead of running forever)