Objects Flashcards
What function prevents data mutation of an object?
Object.freeze(obj);
How do you make a new object, using an existing object as the prototype of the newly created object?
let obj = Object.create(proto, [descriptors])
Example: let animal = { eats: true };
// create a new object with animal as a prototype let rabbit = Object.create(animal);
Technically, we can get/set [[Prototype]] at any time. But usually we only set it once at the object creation time and don’t modify it anymore. Changing a prototype “on-the-fly” with Object.setPrototypeOf or obj.__proto__= is a very slow operation as it breaks internal optimizations for object property access operations.
Can inherited properties of an object be overwritten?
Yes!
What is a JavaScript object?
An object is a collection of properties, and a property is an association between a name (or key) and a value. The object can contain any data types (numbers, arrays, object etc.)
Describe abstraction
Refers to only showing essential details and keeping everything else hidden
Users of the classes should not worry about the inner details of those classes and should not directly interact with other classes’ data
If classes are entangled, then on change creates a ripple effect that causes many more changes
Creating an interface through which classes can interact ensures that each piece can be individually developed
What is the difference between encapsulation and abstraction
Abstraction is about hiding unwanted details while giving out the most essential details, while Encapsulation means hiding the code and data into a single unit e.g. class or method to protect the inner working of an object from the outside world. In other words, Abstraction means extracting common details or generalizing things.
What is polymorphism and what is the difference between static and dynamic polymorphism?
Polymorphism is the ability to call the same method on different objects and have each of them respond in their own way.
Dynamic:
-Occurs during the runtime of the program (when it is being executed)
-Describes when a method signature is in both a subclass and a superclass
-The methods share the same name but have different implementation
-The subclass that the object is an instance of overrides that of the superclass
Static:
-Occurs during compile-time rather than during runtime
-Refers to when multiple methods with the same name but different arguments are defined in the same class
-Despite the methods having the same name, their signatures are different due to their different arguments
What will this print: function Foo(){ console.log(this); }
It prints the window object because this is a global object. Whatever parent scope is, it will be inherited by the child.
let obj1 = {name: 'GreenRoots'}; let obj2 = {name: 'GreenRoots'};
obj1 == obj2 // ???
obj1 === obj2 // ???
Object.is(obj1, obj2); // ???
They all return false because non-primitive data types are passed by reference (unlike primitive types that are passed by value).
Hence both obj1 and obj2 the value at the different memory locations that they are created on.
What will this return in strict mode and non-strict mode? function f1() { return this; }
Non-strict: // In a browser: f1() === window; // true // In Node: f1() === globalThis; // true
Strict:
f1() === undefined; // true
Describe Object Oriented Programming.
OOP is based around the concept of “objects”. These are data structures that contain data fields — known in JavaScript as “properties” — and procedures — known as “methods”.
Some of JavaScript’s in-built objects include Math (used for methods such as random , max and sin ), JSON (used for parsing JSON data), and primitive data types like String , Array , Number and Boolean .
Whenever you rely on in-built methods, prototypes or classes, you are essentially using Object-Oriented Programming
What are the 4 main principles of Object Oriented Programming?
Encapsulation, abstraction, inheritance, and polymorphism
What are prototypes?
Prototypes are the mechanism by which JavaScript objects inherit methods and properties from one another.
let obj = { 3: "test" } What do these return? console.log(obj[3]); console.log(obj["3"]);
They both access the same property, return “test”
How do you determine if there is a property in an object?
“key” in object
Returns true or false
The left side of in there must be a property name. That’s usually a quoted string.
If we omit quotes, that means a variable, it should contain the actual name to be tested.
let user = { age: 30 }; let key = "age";
alert(key in user);
or
alert(“age” in user);