OOP_RSE_Javascript Flashcards

Learn advanced JavaScript (59 cards)

1
Q

What kind of JavaScript OOP inheritance applies to the ES6 classes?

A

Prototypal inheritance

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

ES6 classes&raquo_space;> can they be defined by declaring them or by using an expression?

A
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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you access a class’ name ?

A

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”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What happens if you declare multiple constructors in an ES6 class?

A

There is only one constructor in a class, so you will get an “Syntax Error” error

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you use the “super” keyword in an ES6 class?

A

The super keyword can be used within an object’s constructor to call the constructor of its superclass.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the difference in defining ES5 vs ES6 prototype methods in a class?

A
In ES5, you had to use the "prototype" keyword:
     Animal.prototype.bite(...){...}'
In ES6: 
     class {
          ......
          bite( ) { ....}
     }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What kind of methods can you declare in an ES6 class?

A

Prototypal and static

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What happens if you try to access a static class method from an instance in ES6?

A

TypeError: “yourStaticMethod” is not a function

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What happens if you redeclare a class in ES6?

A

Syntax Error: identifier “myClass” already declared

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What do you have to do in an ES6 inherited class before you use “this”?

A

You have to use “super()”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What kind of property is the “prototype” property for a JavaScript object?

A

It is a private property

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you create privacy in object literals?

A
By using IIFEs. for ex.:
var myObj = (function( ) { var ... } ( ) );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you create privacy in object literals?

A
By using IIFEs. for ex.:
var myObj = (function( ) { var ...; myObj = { } ( ) );
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you declare private vars in a class?

A

You declare them with “var myVar = …” in the constructor; the ones declared with this.myVar will become public

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

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?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How do you define private methods in JavaScript?

A

By declaring the corresponding functions inside the constructor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

How do you make the object available to the private methods inside the object? Why is it that you have to use that way?

A

By using: “var that = this” inside the constructor. Because Crockford’s pattern, “this” will point to global inside inner private functions in an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Can you call private methods from public methods in an ES6 class?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How do you define privileged methods in ES6 and what are they?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Are the privileged methods visible to the public methods inside an ES6 class?

A

Yes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q
In an object literal: 
var jane = {name: "Jane",
                  describe: function(){console.log(this)},
what is the "this" in the describe function pointing to?
A

is pointing to the actual object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Can you delete an inherited property in an Object?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Can an ES6 class’s body include data properties?

A

No, as the class’s body is actually the prototype, so prototypes having data properties are an anti-pattern

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Can an ES6 class’s body include data properties?

A

No, as the class’s body is actually the prototype, so prototypes having data properties are an anti-pattern
A class’s body can only contain methods!!!

25
What is the boolean's value of the following statement and why? Foo === Foo.prototype.constructor
True, because the Foo.prototype.constructor points back to the Foo function
26
In how many ways can super( ) can be used in a class?
Method definitions (in object literals or classes, with or without static) use it like property references (super.prop) or method calls (super.method(···)), in order to refer to super-properties (line B).
27
Does an inherited class actually inherit the static properties/methods of the parent class?
``` yes. As an example: class Foo { static classMethod() { return 'hello'; } } ``` ``` class Bar extends Foo { } Bar.classMethod(); // 'hello' ```
28
When is it NOT necessary to call super( ) in a class constructor?
``` When you override the constructor result: class Foo { constructor() { return Object.create(null); } } console.log(new Foo() instanceof Foo); // false ```
29
What happens if you do not specify a constructor in ES6 classes?
It will use a default constructor: | constructor( ) { }
30
What is the default constructor for the derived classes?
constructor(...args){ super(...args); }
31
How can you define you own error class in ES6?
``` class MyError extends Error{ }; throw new MyError. ```
32
How do you define prototype methods in ES6 classes?
Just define a function inside the class, but outside the constructor: ``` class C { m() {} } ```
33
Can you use prototype methods as constructors in ES6?
``` No; class C { m() {} } let a = new C.prototype.m(); // TypeError ```
34
How are static methods in an ES6 class (in terms of enumerability, writability, configurability)?
Static methods Foo.* are writable and configurable, but not enumerable.
35
How is the prototype of an ES6 class (in terms of enumerability, writability, configurability)?
Foo.prototype is non-writeable, non-enumerable, non-configurable.
36
How is the constructor of an ES6 class (in terms of enumerability, writability, configurability)?
Foo.prototype.constructor is non-writeable, non-enumerable, non-configurable.
37
How are the prototype methods in an ES6 class (in terms of enumerability, writability, configurability)?
Prototype methods Foo.prototype.* are writable and configurable, but not enumerable.
38
What is the prototype of a base class?
Function.prototype
39
When is the instance object created in ES5vs ES6?
The instance object is created in different locations in ES6 and ES5: In ES6, it is created in the base constructor, the last in a chain of constructor calls. In ES5, it is created in the operand of new, the first in a chain of constructor calls.
40
Can you call super( ) twice in a derived class constructor?
No. | Once this is initialized, calling super() produces a ReferenceError. This protects you against calling super() twice.
41
What is the return value of a constructor if it doesn't have a "return" statement?
If a constructor returns implicitly (without a return statement), the result is "this"
42
When do I have to initialize "this" in a constructor that returns an object?
If a constructor explicitly returns an object, it is used as its result. Then it doesn’t matter whether this is initialized or not.
43
Can you subclass built-in constructors in ES5? What about ES6?
Not in ES5, yes in ES6.
44
How do you create a non-deletable object property?
Object.defineProperty(obj, "myProp") ,{ value: ....... configurable: false }
45
How do you convert "arguments" in a function to an Array?
var args = Array.prototype.slice.call(arguments);
46
Are objects passed around in JavaScript by reference or by value?
By reference, they are never copied.
47
What is the best way to dereference on object in JavaScript?
myObj = null;
48
What is the prototype of literal objects?
Object.prototype
49
What is "this" pointing to in a prototype chain?
It is always the object where the search began, not the object where the search actually found the method
50
How do you create an object without prototype?
let d = Object.create(null)
51
How do you check if a property actually exists in an object?
({ }.hasOwnProperty.call(obj,prop));
52
How do you change a property in the prototype chain?
``` You have to first find the object in the prototype chain that actually has the property, and only then to delete it. Something like: delete getDefiningObject(obj,'myProp'); ```
53
What happens to the objects down the proto chain if I modify a prototype?
The proto chain will reflect the change immediately across the whole chain
54
Object.keys() ==> which of the following cases is actually covering: 1. Enumerable / Non-enumerable; 2. Own / inherited properties.
Own/enumerable
55
Object.getOwnPropertyNames() ==> which of the following cases is actually covering: 1. Enumerable / Non-enumerable; 2. Own / inherited properties.
Own/enumerable and non-enumerable
56
What is "for var ... in ... " actually considering? Enumerable/non-enumerable and/or own/inherited properties?
enumerable/ own & inherited
57
How are the properties you create on an object by default - enumerable or non-enumerable? Are they going to appear in (for var ... in ... )?
They are enumerable, and yes, they will come up in "for var ... in ... )
58
How do you check if a property actually exists in an object or in the inherited chain?
propkey in obj
59
Are the getters() and setter() actually inherited from the prototype or not?
Yes they are