questions Flashcards
What kinds of approaches do you know to determine type in js?
typeof is good for primitives for example number returns number, boolean returns boolean, undefined returns undefined, function returns function . But null returns object.
objects, arrays, dates all return object.
There a special way for Arrays -
Array.isArray (name) - boolean.
In the old days, there was a less secure and less accurate way Duck Typing it`s just a test to find if the variable has property or method which belongs to some particular data structures for example Array.slice
What is the difference between variable hoisting and function declaration hoisting?
Function hoisting is happening together with its body. For variables, it`s just a name.
How can we create an object in js?
- just with {} - creates a regular object
- Object.create() - method creates a new object, using an existing object as the prototype
- new operator - creates a new object, from function constructor or new class.
What is an object descriptor? What kinds of descriptor properties do you know?
Descriptor is an object that describes property behavior. We can use Object.defineProperty() method for that. this method can define a new property directly on an object, or modify an existing property on an object, and return the object. I haven’t used it yet in my practice.
configurable - boolean. Property descriptor may be changed and if the property may be deleted.
enumerable - boolean. If this property shows up during enumeration
value - Can be any valid JavaScript value (number, object, function, etc)
writable - boolean. Can the property be changed or not
get - function where you can create a custom logic how and what will be returned when someone will try to get value of this property.
set - function where you can create a custom logic how and what will be stored for that property.
Explain how prototypal inheritance works. How to implement class inheritance in js? Could you provide an example?
inheritance in JS is a process when a child class inheritance features from a parent class. We can implement it by using the keyword "extends". class A { constractor(name) { this.name = name; } say( ) { console.log(this.name); } } class B extends A { walk( ) { ...... } }
const bubka = new B(“Bubka”);
What will happen when we return an object or a primitive value from a constructor function?
It should return the Context by default when we are using a “new” keyword
What kinds of other strategies except inheritance do you know to implement reusing of the code?
By using Context (“this”) methods.
Call, Apply or Bind
Inheritance
extend
What does the “new” keyword do? prototype vs __proto__ vs [[Prototype]]?
it can:
- Creates a blank, plain JavaScript object.
- Adds __proto__ property which links to the constructor function’s prototype.
- Binds methods from the constructor functions prototype to the newly created object.
- Returns the newly created object.
What is an “array” in javascript? What is the best way to clone an array?
Array is an object that stores values inside. One Array can store values of different types for example string, number, and boolean can be stored in one Array.
You can clone it by using:
.slice() - method returns a copy of a portion of an array into a new array.
.concat() - method is used to merge two or more arrays.Creates a new Array.
Array.from() - static method creates a new, copied Array instance from an array-like.