JS 2 Flashcards
What is a method?
a function which is a property of an object
How can you tell the difference between a method definition and a method call?
- a method definition has function definition and code block
- a method call is like a function call with object name dot and method name followed by parenthesis
Describe method definition syntax (structure).
- defined as a property with a value of a function in an object literal {methodName: function () {}}
- after the object’s creation, a function could be assigned to the method name’s property of the object objectName.methodName = function () {}
Describe method call syntax (structure).
object name, dot, method name, parenthesis
objectName.methodName()
How is a method different from any other function?
need dot or bracket notation to see what object the method in
What is the defining characteristic of Object-Oriented Programming?
- objects can contain both properties and methods
- data and behavior are combined
What are 4 principles of OOP?
Abstraction
Encapsulation
Inheritance
Polymorphism
What is “abstraction”?
- removing temporal details to focus on details of greater importance
- able to work with complex process in simple ways
What does API stand for?
application programming interface
What is the purpose of an API?
- allows users to use interface independently of implementation
- can use something without knowing how it was built
What is ‘this’ in JavaScript?
a property of an execution context that is a reference to its enclosing object
What does it mean to say that ‘this’ is an “implicit parameter”?
‘this’ is available in a function’s code block, even though it was never explicitly declared
When is the value of this determined in a function; call time or definition time?
call time
How can you tell what the value of this will be for a particular function or method definition?
you don’t know until it is called
How can you tell what the value of this is for a particular function or method call?
- if it is in a method inside an object, then it refers to that object
- if it is in a function inside the global scope, then it refers to Window
What kind of inheritance does the JavaScript programming language use?
prototypical inheritance, which is reusing objects as prototypes to allow for the inheritance of methods and properties
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?
those methods are defined on those elements’ __proto__ property
If an object does not have its own property or method by a given key, where does JavaScript look for it?
looks for it on the prototype object which it is inheriting properties/methods from
What does the new operator do?
- creates an instance object from a constructor function
- sets prototype for that object
- passes the object as “this”
- object that was created is returned
What property of JavaScript functions can store shared behavior for instances created with new?
prototype property
What does the instanceof operator do?
- tests to see if the prototype property of a constructor appears in the prototype chain of an object
- returns a boolean whether it is an instance or not