Skip to content

Instantly share code, notes, and snippets.

@eamon0989
Last active July 29, 2021 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eamon0989/a82b03b64d53e9d41454655d3712ebf3 to your computer and use it in GitHub Desktop.
Save eamon0989/a82b03b64d53e9d41454655d3712ebf3 to your computer and use it in GitHub Desktop.
function objectFactory() {
return {
property1: 'My first property',
property2: 'My second property',
printProperties() {
for (let prop in this) {
if (typeof this[prop] !== 'function') {
console.log(`${prop}: ${this[prop]}`);
}
}
}
};
}
let object = objectFactory();
let object1 = objectFactory();
console.log(object);
console.log(object1);
/*
{
property1: 'My first property',
property2: 'My second property',
printProperties: [Function: printProperties]
}
{
property1: 'My first property',
property2: 'My second property',
printProperties: [Function: printProperties]
}
*/
console.log(Object.getPrototypeOf(object) === Object.prototype); // true
console.log(Object.getPrototypeOf(object1) === Object.prototype); // true
console.log(object instanceof objectFactory); // false
Object.setPrototypeOf(object, objectFactory.prototype);
console.log(object instanceof objectFactory); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment