Week 4 Flashcards
What is a method?
A method is a function that is the property of an object
How can you tell the difference between a method definition and a method call?
Method definition has the function keyword and a code block vs the method call has dot notation and the object we are calling it on
Describe method definition syntax (structure)
property name: function (parameters) {
codeblock
}
Describe method call syntax (structure).
object.methodName(parameters, if any)
How is a method different from any other function?
a method requires the object you are calling it on
What is the defining characteristic of object-oriented programing?
it contains both data and behavior
data in the form of attributes/properties
code in the form of procedures/methods
What are the 4 principles of Object-Oriented Programming?
Abstraction, encapsulation, inheritance, and polymorphism
What is “abstraction”?
Abstraction allows us to work with complex things in simple ways
ex: light switch, DOM
What is the purpose of an API?
It’s a way for computer programs to communicate with each other
API is consisted of abstractions created to allow you to interact with a complex system
What is this in JavaScript?
It’s an implicit parameter available in any function
What does it mean to say that this is an “implicit parameter”?
It’s available in a function’s code block even though it was never included in the list of parameters or declared as a variable
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 is nothing because the function is not being called
var character = {
firstName: ‘Mario’,
greet: function () {
var message = ‘It's-a-me, ‘ + this.firstName + ‘!’;
console.log(message);
}
};
Given the above character object, what is the result of the following code snippet? Why?
character.greet();
the answer is ‘It’s-a-me, Mario” because the value of this is the object character. character.firstName = Mario
var character = {
firstName: ‘Mario’,
greet: function () {
var message = ‘It's-a-me, ‘ + this.firstName + ‘!’;
console.log(message);
}
};
Given the above character object, what is the result of the following code snippet? Why?
var hello = character.greet;
hello();
The result is ‘It’s-a-me, undefined” because when the function is being called, there is no object directly to the left of it. By default, the value of object is the window object. Window object does not have a firstName property so it is undefined
How can you tell what the value of this will be for a particular function/method definition
You can’t because the function/method is not being called
How can you tell what the value of this is for a particular function or method call?
Look to the left of the function call. The value of this will be the object to the left. If there is no object, the default value will be the window object
What kind of inheritance does the JavaScript programming language use?
Prototype-based / Prototypal inheritance
What is a prototype in JavaScript?
An object that contains properties and methods that can be used by other objects
How is it possible to call methods on strings, arrays, and numbers even though those methods don’t actually exist on strings, arrays, and numbers?
Because of prototype objects
If an object does not have its own property or method by a given key, where does JavaScript look for it?
inside the object’s prototype object
What does the new operator do?
- Creates a blank, plain javascript object
- Assigns the value of the prototype property of the function as the value of the __ proto__ property of the new object
- constructor function is called with the given arguments and the value of this becomes the object that was created
- The object becomes the return value
What property of JavaScript functions can store shared behavior for instances created with new?
prototype property
What does the instanceof operator do?
returns boolean
Tests the presence of constructor.prototype in an object’s prototype chain
long story short:
basically checks to see if object was created with a constructor function