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);
How do we make a clone of an object?
Object.assign(dest, [src1, src2, …])
Example: let user = { name: "John", age: 30 };
let clone = Object.assign({}, user);
Keep in mind if a property is an object, you will l have the same problem of copying a reference. To fix that, we should use a cloning loop that examines each value of user[key] and, if it’s an object, then replicate its structure as well. That is called a “deep cloning”.
function makeUser() { return { name: "John", ref: this }; }
let user = makeUser();
alert( user.ref.name ); // What’s the result?
If in strict mode….Error: Cannot read property ‘name’ of undefined
Below works because user.ref() is a method. And the value of this is set to the object before dot .
function makeUser() { return { name: "John", ref() { return this; } }; }
let user = makeUser();
alert( user.ref().name ); // John
What kind of type(s) can be object property keys?
String and symbol
How can you check if a property of an object is inherited or not?
hasOwnProperty()
Example: function Phone() { this.operatingSystem = 'Android'; } const myPhone = new Phone();
console.log(myPhone.hasOwnProperty('operatingSystem')); // true
How can you can confirm if a particular object serves as the prototype of another object?
isPrototypeOf()
Example: const rodent = { hasTail: true }; function Mouse() { this.favoriteFood = 'cheese'; }
Mouse.prototype = rodent; const ralph = new Mouse();
console.log(rodent.isPrototypeOf(ralph)); // true
What do you get when you use the constructor property on an object?
It returns a reference to the constructor function that created that object in the first place.
Example: function Longboard() { this.material = 'bamboo'; }
const board = new Longboard();console.log(board.constructor);
// function Longboard() { // this.material = 'bamboo'; // }
If an object was created using literal notation, its constructor is the built-in Object() constructor function!
How do you confirm an object’s prototype?
Object.getPrototypeOf()
Let’s say we want a Child object to inherit from a Parent object. Why shouldn’t we just set Child.prototype = Parent.prototype
Objects are passed by reference. This means that since the Child.prototype object and the Parent.prototype object refer to the same object – any changes you make to Child’s prototype will also be made to Parent’s prototype! We don’t want children being able to modify properties of their parents!
On top of all this, no prototype chain will be set up. What if we want an object to inherit from any object we want, not just its prototype?
What does instanceof operator do?
The instanceof operator tests to see if the prototype property of a constructor appears anywhere in the prototype chain of an object. The return value is a boolean value.