Week 4 Flashcards
What is a method?
A method is a function that is the property of an object
How can you tell the difference between a method definition and a method call?
Method definition has the function keyword and a code block vs the method call has dot notation and the object we are calling it on
Describe method definition syntax (structure)
property name: function (parameters) {
codeblock
}
Describe method call syntax (structure).
object.methodName(parameters, if any)
How is a method different from any other function?
a method requires the object you are calling it on
What is the defining characteristic of object-oriented programing?
it contains both data and behavior
data in the form of attributes/properties
code in the form of procedures/methods
What are the 4 principles of Object-Oriented Programming?
Abstraction, encapsulation, inheritance, and polymorphism
What is “abstraction”?
Abstraction allows us to work with complex things in simple ways
ex: light switch, DOM
What is the purpose of an API?
It’s a way for computer programs to communicate with each other
API is consisted of abstractions created to allow you to interact with a complex system
What is this in JavaScript?
It’s an implicit parameter available in any function
What does it mean to say that this is an “implicit parameter”?
It’s available in a function’s code block even though it was never included in the list of parameters or declared as a variable
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);
}
};
this is nothing because the function is not being called
var character = {
firstName: ‘Mario’,
greet: function () {
var message = ‘It's-a-me, ‘ + this.firstName + ‘!’;
console.log(message);
}
};
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
the answer is ‘It’s-a-me, Mario” because the value of this is the object character. character.firstName = Mario
var character = {
firstName: ‘Mario’,
greet: function () {
var message = ‘It's-a-me, ‘ + this.firstName + ‘!’;
console.log(message);
}
};
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();
The result is ‘It’s-a-me, undefined” because when the function is being called, there is no object directly to the left of it. By default, the value of object is the window object. Window object does not have a firstName property so it is undefined
How can you tell what the value of this will be for a particular function/method definition
You can’t because the function/method is not being called