JavaScript OOP Flashcards
What is a method?
A method is a function which is a property of an object. There are two kinds of methods: instance methods which are built-in tasks performed by an object instance, or static methods which are tasks that are called directly on an object constructor.
How can you tell the difference between a method definition and a method call?
A method definition will have a colon (:) following the name followed by an anonymous function for that method and a method call will have a set of parentheses with some or none parameters following the name of the method.
Describe method definition syntax (structure).
The method name followed by colon, then anonymous function called with parameters, then curly braces for what the function will do, including a return. This method is defined inside of an object as a property.
Describe method call syntax (structure).
The object name followed by a period (‘.’) followed by the name of the method with a set of parentheses ( ‘()’ ) filled with either some or no parameters.
How is a method different from any other function?
A method is a function which is a property of an object and must be called with that object.
What is the defining characteristic of Object-Oriented Programming?
That objects can contain both data (as properties) and behavior (as methods).
What are the four “principles” of Object-Oriented Programming?
Abstraction, encapsulation, inheritance, and polymorphism
What is “abstraction”?
Making complex or specific details into a more abstract object or idea that focuses on the bigger picture instead of details.
What does API stand for?
Application programming interface.
What is the purpose of an API?
To give programmers a way to interact with a system in a simplified, consistent fashion (abstraction).
What is this in JavaScript?
this is an implicit parameter of all JavaScript functions.
the value of this is determined at call time.
the value of this can be recognized as “the object to the left of the dot” when the function is called (as a method).
if there is no value to the left of the dot when the function is called, then by default, this will be the global window object.
if you cannot see the function being called, then you do not know what the value of this will be.
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?
The value of this is determined at the call time of a function
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 refers to the character object when it will be called, but since the code snippet is a definition, it refers to nothing and defaults to the window object.
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
‘It’s-a-me, Mario!’
This is because the greet method of the character object is being called. The greet method concatenates this.firstName, which would be the value of the firstName property of the character object since it is what is left of the dot when the function is called, with other strings.