Skip to content

Instantly share code, notes, and snippets.

@nirabpant
Last active October 26, 2021 03:39
Show Gist options
  • Save nirabpant/035aab17f12c74e5505a9981ea6712ce to your computer and use it in GitHub Desktop.
Save nirabpant/035aab17f12c74e5505a9981ea6712ce to your computer and use it in GitHub Desktop.
//Example 3
const arr = ['a', 'b'];
// Arrays have a type of 'object' and in addition to the
// explicitly declared properties and values, it can also access
// built-in methods through inheritance along its prototype chain
console.log(typeof arr); // 'object'
console.log(arr[0]); // 'a'
console.log(arr[1]); // 'b'
console.log(arr.join('')); // 'ab'
// Validate that 'arr' has a copy of the '0' and '1' properties but not the
// 'join' method that it inherits from its prototype
console.log(arr.hasOwnProperty(0)); // true
console.log(arr.hasOwnProperty(1)); // true
console.log(arr.hasOwnProperty('join')); // false
console.log(Object.getPrototypeOf(arr).hasOwnProperty('join')); // true
// Validate that an array literal will have 'Array.prototype'
// as its immediate parent in the prototype chain, and that
// the 'constructor' property points to the 'Array' constructor object
console.log(Object.getPrototypeOf(arr) === Array.prototype); // true
console.log(arr.__proto__ === Array.prototype); // true
console.log(arr.constructor === Array); // true
// confirm that only function objects have a 'prototype' property by default
console.log(arr.hasOwnProperty('prototype')); // undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment