Module 2 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?
Methods are defined within the object literal in a series of function definitions, a method is called as a property of the object called when optional parameters are included in the parenthesis
Describe method definition syntax (structure).
Methods are defined in the object literal using a method name : function (parameters) { code block for function definition }
or using the dot notation and assigning it a value
can see that there is a defined body to that method
Describe method call syntax (structure).
object.methodName(parameters);
How is a method different from any other function?
It is attached to an object and must be called as a method of the object that describes the behavior of the object (property of the object)
What is the defining characteristic of Object-Oriented Programming?
Objects contain both data (properties) and behavior (methods)
take related data and functionality and wrap them together in something called an object
What are the four “principles” of Object-Oriented Programming?
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
would add a fifth called XYZ
What is “abstraction”?
the most important part of the term “abstraction” boils down to being able to work with (possibly) complex things in simple ways.
wrapping complex tasks in simple interfaces
What does API stand for?
application programming interface
What is the purpose of an API?
the purpose of every software API is to give programmers a way to interact with a system in a simplified, consistent fashion: aka, an abstraction
What is this in JavaScript?
an implicit parameter of all javascript functions
a JS keyword that references the current object you are in
What does it mean to say that this is an “implicit parameter”?
meaning 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
implied parameter - changed based on how you call the method but it is implied bc you don’t actually list it out in the parameter list
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
It’s-a-me, Mario! because this refers to the character 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!
because the function is not being called with the object this is referring to to the left of the dot
How can you tell what the value of this will be for a particular function or method definition?
You cannot, you can only tell the value of this when the function is called
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 the function is not being called with the object this is referring to to the left of the dot
because this then refers to the window, not the object
How can you tell what the value of this will be for a particular function or method definition?
You cannot, you can only tell the value of this when the function is called
How can you tell what the value of this is for a particular function or method call?
Look for an object to the left of the dot and if there is none, it will default to the window object
if not in the global scope, it will be undefined
What is a prototype in JavaScript?
an object that contains primarily methods and properties that can be used by other objects
What is a prototype in JavaScript?
an object that contains primarily methods and properties that can be used by other objects
property lookup on an object if the object doesn’t have it, it looks to that object’s prototype to see if that has the property
If an object does not have its own property or method by a given key, where does JavaScript look for it?
to the prototype object
If an object does not have its own property or method by a given key, where does JavaScript look for it?
to the prototype of the current object
What does the new operator do?
- Creates a blank, plain JavaScript object.
- Adds a property to the new object (__proto__) that links to the constructor function’s prototype object
- Binds the newly created object instance as the this context (i.e. all references to this in the constructor function now refer to the object created in the first step).
- Returns this if the function doesn’t return an object.
- Creates a blank object
- Sets that object’s prototype (__proto__) to equal the prototype property of the function it is called on (constructor)
- Runs the constructor function with ‘this’ that referring to new object
- Returns the return value of the function or the constructor function or if undefined, gives you back that new object
What property of JavaScript functions can store shared behavior for instances created with new?
__proto__
prototype property