JSFiddle - React, Tailwind, and code Playground
objects and prototypal inheritance
by Matthew Day
JavaScript
'use strict';
// Object Literal
var spy1 = {
name: "James Bond",
age: 44
}
//objectLiteral.__proto__ = {
// licensed: true,
//}
console.log('SPY1 --- OBJECT LITERAL');
console.log(spy1);
// Constructor
function Spy2(name, age) {
this.name = name;
this.age = age;
}
//Spy2.prototype = {
// licensed: true
//}
var james = new Spy2("Ethan Hunt", 36);
console.log('SPY2 --- CONSTRUCTOR');
console.log(james);
// Object Create
var spy3 = Object.create(Object.prototype, {
name: {
configurable: true,
enumerable: true,
writable: true,
value: 'Jason Bourne'
},
age: {
configurable: true,
enumerable: true,
writable: true,
value: 32
}
});
console.log('SPY3 --- CREATE');
console.log(spy3);
// Classical Inheritance
class Spy4 {
constructor(name, age) {
this.name = name;
this.age = age;
}
}
var nikita = new Spy4('Little Nikita', 26);
console.log('SPY4 --- CLASS');
console.log(nikita);