Week 5 Flashcards
What is a method?
A method is a function which is a property of an object.
How can you tell the difference between a method definition and a method call?
Method definition is the function inside an object which has a name followed by a colon, while method call is when you call its method with arguments or without arguments
Describe method definition syntax (structure).
First you declare the object by creating a variable and equaling it to curly braces. Within the curly braces you have properties which can be given any name and then a colon. Following the colon you have a function. If you want to add more properties you have to add a comma at the end of the closing curly brace. If not, close the object with a closing curly brace followed by a semicolon.
Describe method call syntax (structure).
You call the object name followed by a period, then the name of the property followed by parentheses with arguments if it has any.
How is a method different from any other function?
A method is associated with a object and functions are not.
What is the defining characteristic of Object-Oriented Programming?
Objects can contain both data (as properties) and behavior (as methods).
What are the four “principles” of Object-Oriented Programming?
Abstraction, Encapsulation, Inheritance, Polymorphism
What is “abstraction”?
Being able to work with (possibly) complex things in simple ways.
What does API stand for?
Application Programming Interface
What is the purpose of an API?
The purpose of API is to communicate between apps with each other as intermediary, where both apps might have been built with different tools and technologies.
What is this in JavaScript?
It is an implicit parameter of all JavaScript functions. The value of this is determined by how a function is called.
What does it mean to say that this is an “implicit parameter”?
It is available in a function’s code block even though it was never included in the function’s parameter list or declared with 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);
}
};
window until we call it then it will be the character object
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
It’s-a-me, Mario! Because our character object is responsible in calling the greet function.
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();
It’s-a-me, undefined! The hello function is not being called as a method of an object so it defaults to the window object, which does not have this.firstName so it is undefined.
How can you tell what the value of this will be for a particular function or method definition?
You can until the function is called.