Module 2 Flashcards
What is a method?
A function that is a property of an object.
How can you tell the difference between a method definition and a method call?
Function definition has structure function () { }, a call just has the function name and parameters, empty if none.
Describe method definition syntax (structure).
an object, function keyword, parenthese for arguments, and codeblock
Describe method call syntax (structure).
object.methodName()
How is a method different from any other function?
Stored as a property of an object
What is the defining characteristic of Object-Oriented Programming?
objects can contain both data (as properties) and behavior (as methods). You wrap functionality with the data structure (object).
What are the four “principles” of Object-Oriented Programming?
abstraction, encapsulation, inheritance, polymorphism
What is “abstraction”?
Simplifying a complex action into a simple one
What does API stand for?
application programming interface
What is the purpose of an API?
A software interface that offers services to other software. A way for one software to use another software’s data without having to know how the other software works. Like going to a restaurant and ordering food, you don’t need to know how to make it or how to request the chef, just need to know what you want.
What is this in JavaScript?
An implicit parameter that changes depending on where in a function it is called. Usually refers to what object you are in.
What does it mean to say that this is an “implicit parameter”?
It is something available to use in a code block even if it was never explicitly declared or included in its parameter list.
When is the value of this determined in a function; call time or definition time?
Determined when the function is called.
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); } };
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 it calls the method in the object, which calls for a string plus the object firstName property with this.
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!’ Because ‘this’ refers to window since it is not directing to any object. Hello is only assigned the function greet, no connection to character object.
How can you tell what the value of this will be for a particular function or method definition?
Impossible to tell because this is defined by when it is called. You can make a best guess if it is a method.
How can you tell what the value of this is for a particular function or method call?
Find where the function is called and look for an object to the left of the dot.
What kind of inheritance does the JavaScript programming language use?
Prototypal (prototype-based). Objects inherit from other objects.
Trivia: Most other programming languages are class-based, they inherit from classes rather than objects.
What is a prototype in JavaScript?
Template that objects inherit properties and methods from
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on objects, arrays, and numbers?
It has an existing prototype object in javascript
If an object does not have it’s own property or method by a given key, where does JavaScript look for it?
it checks the first prototype then checks the prototype of that prototype and so on, until it finds it, or returns undefined.
What does the new operator do?
It creates a new empty object then assigns that object’s prototype to be the prototype property of the constructor, then calls the constructor function assigning ‘this’, and if there is no return object for the function it uses ‘this’.
What property of JavaScript functions can store shared behavior for instances created with new?
Prototype property. Note that function objects have their own properties that can be found. 5 total properties, see MDN.