4/11 - 4/15/2022 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?
The definition is in the ‘value’ position of an object, after the property. It starts with the ‘function’ keyword.
The method call is done by typing the object’s name followed by the property with dot notation.
Describe method definition syntax (structure).
cons obj = { firstName: Daniel, lastName: Cho, fullName: function () { return this.firstName + " " + this.lastName; } };
The “function()” after “fullName” is an example of method definition syntax.
Describe method call syntax (structure).
Referring to last card, an example of a method call syntax would be obj.fullName();
How is a method different from any other function?
A function is a set of instructions that perform a task.
A method is a set of instructions that are associated with an object.
What is the defining characteristic of Object-Oriented Programming?
The defining characteristic of OOP is that 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 complex things in simple ways.
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: aka, an abstraction.
What is ‘this’ in JavaScript?
‘this’ is an implicit parameter and its value is determined when the function is called. If the function is called by itself (not as a method of an object), the value of ‘this’ is the global window object. When the function is assigned as a method of an object, the value of ‘this’ is the object. If you cannot see where the function (or method) is called, you cannot say for sure what the value of ‘this’ is.
What does it mean to say that ‘this’ is an “implicit parameter”?
To say that ‘this’ is an ‘implicit parameter’ means that 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); } };
‘this’ refers to the object ‘character’. ‘this.firstName’ refers to ‘character.firstName’.
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
‘It’s a me, Mario!’
Because ‘this’ is used in a function that is a method of an object.
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!’
This is the result of the code snippet because you cannot see where the method (character.greet) is being called.
How can you tell what the value of ‘this’ will be for a particular function or method definition?
You can’t. The value of ‘this’ is determined when the function or method is called, not when it’s defined.