Last active
October 23, 2021 22:05
-
-
Save nirabpant/751e85299f649ec92253120fda264960 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Example 1 | |
const obj = { | |
color: "blue", | |
amount: 100, | |
}; | |
// Check the type of the object literal 'obj' | |
console.log(typeof obj); // 'object' | |
// Verify that 'obj' has the properties we expect it to have | |
console.log(obj.hasOwnProperty('color')); // true | |
console.log(obj.hasOwnProperty('amount')); // true | |
console.log(obj.hasOwnProperty('randomProperty')); // false | |
console.log(obj.hasOwnProperty('hasOwnProperty')); // false | |
// Verify that 'obj.__proto__ === Object.getPrototypeOf(obj)' | |
// which are both equal to 'Object.prototype' for an object literal | |
// defined using the curly braces and assigning it to a variable. | |
console.log(Object.getPrototypeOf(obj) === Object.prototype); // true | |
console.log(obj.__proto__ === Object.prototype); // true | |
// Confirm that Object.prototype does indeed have the 'hasOwnProperty' | |
// method defined on it | |
console.log(Object.prototype.hasOwnProperty('hasOwnProperty'); // true | |
// Verify that the constructor of 'obj' is the 'Object' constructor function | |
// and check the type of the 'Object' constructor function which is an object | |
// of type 'function' | |
console.log(obj.constructor === Object); // true | |
console.log(typeof Object); // 'function' | |
// Similarly, the 'constructor' property of 'obj' is actually inherited from | |
// its prototype, Object.prototype | |
console.log(obj.hasOwnProperty('constructor')); // false | |
console.log(Object.prototype.hasOwnProperty('constructor')); // true | |
// Finally, check there is no 'prototype' property on 'obj' | |
// as only function objects have property 'prototype' | |
console.log(obj.prototype); // undefined |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment