Skip to content

Instantly share code, notes, and snippets.

View eamon0989's full-sized avatar

Eamon eamon0989

View GitHub Profile
let obj = {};
console.log(Object.prototype); // [Object: null prototype] {}
console.log(Object.getPrototypeOf(obj)); // [Object: null prototype] {}
console.log(Object.prototype === Object.getPrototypeOf(obj)); // true
console.log(Object.prototype.isPrototypeOf(obj)); // true
Object.prototype.foo = 'Hey there!';
console.log(Object.prototype); // [Object: null prototype] { foo: 'Hey there!' }
let foo = {
bar: 1,
};
console.log(foo.bar); // 1
console.log(foo.hasOwnProperty('bar')); // true
function Factory() {
this.foo = 1;
this.bar = 'string';
}
Factory.prototype.example = "I'm a property of Factory.prototype";
console.log(Factory.prototype); // { example: "I'm a property of Factory.prototype" }
let descendant = new Factory();
console.log(descendant); // Factory { foo: 1, bar: 'string' }
function Factory() {
this.foo = 1;
this.bar = 'string';
}
let descendant = new Factory();
console.log(descendant); // Factory { foo: 1, bar: 'string' }
console.log(Factory.prototype); // {}
// This is a continuation of the previous code block
Object.prototype.test = `I'm a property of Object.prototype`;
console.log(Object.prototype); // [Object: null prototype]
// { test: "I'm a property of Object.prototype" } */
console.log(Factory.prototype.test); // I'm a property of Object.prototype
console.log(Factory.prototype); // {}
console.log(descendant.test); // I'm a property of Object.prototype
function Factory() {
this.foo = 1;
this.bar = 'string';
}
let descendant = new Factory();
console.log(Factory.prototype); // {}
console.log(Object.getPrototypeOf(Factory.prototype)); // [Object: null prototype] {}
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]}`);
}
function Grandfather(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
}
Grandfather.prototype.aboutMe = function() {
console.log(`I'm ${this.name}, I'm ${this.age} years old and I'm ${this.job === 'retired' ? '' : 'a '}${this.job}.`);
};
class Grandfather {
constructor(name, age, job) {
this.name = name;
this.age = age;
this.job = job;
}
aboutMe() {
console.log(`I'm ${this.name}, I'm ${this.age} years old and I'm ${this.job === 'retired' ? '' : 'a '}${this.job}.`);
}
let name = 'Eamon';
function printName() {
console.log(name);
}
printName(); // => 'Eamon'
name = 'Jack';
printName(); // => 'Jack'