by Kiril Knysh
Class is a schema
Object is a `real` instance of schema
unordered dynamic collection of properties (key-value pairs)
var obj = { prop0: 0 };
obj.prop1 = 1;
var func = function() {};
func.prop1 = 1;
var arr = [1, 2, 4];
arr.prop1 = 1;
function legoMan(name) {
return {
name: name,
say: function(message) {
console.log(this.name + ': "' + message + '"');
}
};
}
var alex = legoMan('Alex');
alex.say('Hello, Kattie!'); // Alex: "Hello, Kattie!"
var kattie = legoMan('Kattie');
kattie.say('No'); // Kattie: "No"
function LegoMan(name) {
this.name = name;
this.say = function(message) {
console.log(this.name + ': "' + message + '"');
}
}
var alex = new LegoMan('Alex');
alex.say('Hello, Kattie!'); // Alex: "Hello, Kattie!"
var kattie = new LegoMan('Kattie');
kattie.say('No'); // Kattie: "No"
function LegoMan(name) {
this.name = name;
}
LegoMan.prototype.say = function(message) {
console.log(this.name + ': "' + message + '"');
}
var alex = new LegoMan('Alex');
alex.say('Hello, Kattie!'); // Alex: "Hello, Kattie!"
var kattie = new LegoMan('Kattie');
kattie.say('No'); // Kattie: "No"
function LegoMan(name) {
this.name = name;
}
LegoMan.prototype.say = function(message) {
console.log(this.name + ': "' + message + '"');
}
var alex = new LegoMan('Alex');
class LegoMan {
constructor(name) {
this.name = name;
}
say(message) {
console.log(this.name + ': "' + message + '"');
}
}
const alex = new LegoMan('Alex');
alex.say('Hello, Kattie!'); // Alex: "Hello, Kattie!"
const kattie = new LegoMan('Kattie');
kattie.say('Hello!'); // Kattie: "Hello!"
// TypeError: Class constructor LegoMan cannot be invoked without 'new'
const alex = LegoMan('Alex');
// SyntaxError: A class may only have one constructor
class LegoMan {
constructor(name) { }
constructor(name) { }
}
// ReferenceError: LegoMan is not defined
const alex = new LegoMan('Alex');
class LegoMan {
constructor(name) {
this.name = name;
}
}
LegoMan.prototype.constructor === LegoMan; // true
class LegoMan {
constructor(name) {
this.name = name;
this.age = 0;
}
set newAge(value) {
this.age = value;
}
get represent() {
return `My name is ${this.name}. I am ${this.age} years old.`;
}
}
const alex = new LegoMan('Alex');
alex.represent // My name is Alex. I am 0 years old.
alex.newAge = 18;
alex.represent // My name is Alex. I am 18 years old.
class LegoMan {
constructor(name) {
this.name = name;
}
static getInfo(man) {
return `This is ${man.name}.`;
}
}
const alex = new LegoMan('Alex');
LegoMan.getInfo(alex); // This is Alex
alex.getInfo // undefined
typeof 132 // "number"
typeof 2.71 // "number"
typeof 'Alex' // "string"
typeof LegoMan // "function"
typeof true // "boolean"
typeof {} // "object""
typeof NaN // "number"
typeof new Number(132) // "object"
typeof [1, 2, 3] // "object", Array.isArray should be used
typeof null // "object"
class LegoMan {
constructor(name) {
this.name = name;
}
}
const alex = new LegoMan('Alex');
alex instanceof LegoMan // true
alex instanceof Object // true
alex instanceof LegoMan
LegoMan[@@hasInstance](alex)
function extend(Child, Parent) {
var F = function() { }
F.prototype = Parent.prototype;
Child.prototype = new F();
Child.prototype.constructor = Child;
Child.superclass = Parent.prototype; // optional
}
function extend(Child, Parent) {
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
Child.superclass = Parent.prototype; // optional
}
function extend(Child, Parent) {
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
Child.superclass = Parent.prototype; // optional
}
function LegoBatMan(name) {
LegoBatMan.superclass.constructor.call(this, name);
}
extend(LegoBatMan, LegoMan);
class LegoBatMan extends LegoMan {
constructor(name) {
super(name);
}
say(message) {
console.log('|\\__/|');
super.say(message);
console.log('|\\__/|');
}
}
const bruce = new LegoBatMan('Bruce');
bruce.say('this city needs a hero');
// |\__/|
// Bruce: "this city needs a hero"
// |\__/|