OOP_RSE_Javascript Flashcards
Learn advanced JavaScript
What kind of JavaScript OOP inheritance applies to the ES6 classes?
Prototypal inheritance
ES6 classes»_space;> can they be defined by declaring them or by using an expression?
They can be defined by both: Declaration: class Bird{...} Expression: a. unnamed class expression: let animal = class { ... }; b. named class expression: let animal = class Animal{...};
How do you access a class’ name ?
If you use a named class expression, the “name” attribute will become a property on the class’s body and can be accessed using “.name”
What happens if you declare multiple constructors in an ES6 class?
There is only one constructor in a class, so you will get an “Syntax Error” error
How do you use the “super” keyword in an ES6 class?
The super keyword can be used within an object’s constructor to call the constructor of its superclass.
What is the difference in defining ES5 vs ES6 prototype methods in a class?
In ES5, you had to use the "prototype" keyword: Animal.prototype.bite(...){...}' In ES6: class { ...... bite( ) { ....} }
What kind of methods can you declare in an ES6 class?
Prototypal and static
What happens if you try to access a static class method from an instance in ES6?
TypeError: “yourStaticMethod” is not a function
What happens if you redeclare a class in ES6?
Syntax Error: identifier “myClass” already declared
What do you have to do in an ES6 inherited class before you use “this”?
You have to use “super()”;
What kind of property is the “prototype” property for a JavaScript object?
It is a private property
How do you create privacy in object literals?
By using IIFEs. for ex.: var myObj = (function( ) { var ... } ( ) );
How do you create privacy in object literals?
By using IIFEs. for ex.: var myObj = (function( ) { var ...; myObj = { } ( ) );
How do you declare private vars in a class?
You declare them with “var myVar = …” in the constructor; the ones declared with this.myVar will become public
Can you access private class’s vars from the Object’s own public methods? if yes, how, if no, how do you still access them?
No, you cannot access private vars from an object’s public methods. You can access the private vars from the objects’s private methods though.
How do you define private methods in JavaScript?
By declaring the corresponding functions inside the constructor
How do you make the object available to the private methods inside the object? Why is it that you have to use that way?
By using: “var that = this” inside the constructor. Because Crockford’s pattern, “this” will point to global inside inner private functions in an object.
Can you call private methods from public methods in an ES6 class?
No
How do you define privileged methods in ES6 and what are they?
use “this.myMethod” inside the constructor to define a privileged method; they are used to access the private methods from the outside of the object
Are the privileged methods visible to the public methods inside an ES6 class?
Yes
In an object literal: var jane = {name: "Jane", describe: function(){console.log(this)}, what is the "this" in the describe function pointing to?
is pointing to the actual object
Can you delete an inherited property in an Object?
No
Can an ES6 class’s body include data properties?
No, as the class’s body is actually the prototype, so prototypes having data properties are an anti-pattern