Skip to content

Instantly share code, notes, and snippets.

Agata agatablue

View GitHub Profile
View Milion Cats - and Cat.prototype
function Cat(name) {
this.name = name;
}
Cat.prototype.getVoice = function() {
console.log("Miau miau");
};
Cat.prototype.catFeet = 4;
var numberOfCats = 1000000;
var arrayOfCats = [];
View Milion Cats
function Cat(name) {
this.name = name;
this.catFeet = 4;
this.getVoice = function() {
console.log("Miau miau");
};
}
var numberOfCats = 1000000;
var arrayOfCats = [];
View What new do - very simply example
function New(func) {
//1. create empty object
var res = {};
//2. set his [[Prototype]] (aka __proto__) to function property prototype
res.__proto__ = func.prototype;
//3. It makes the this variable point to the newly created object
func.call(res);
View Constructor Cat
function Cat(name) {
this.name = name;
this.catFeet = 4;
this.getVoice = function() {
console.log("Miau miau");
};
}
var filemon = new Cat("Filemon");
var mruczek = new Cat("Mruczek");
View cat object
var cat = {
getVoice: function() {
console.log("Miau miau");
}
};
cat.getVoice(); // Miau miau
View Concatenative inheritance
const alien = {
sayHello () {
return `Hello, my name is ${ this.name }`;
}
};
const clark = Object.assign(
{},
alien,
{name: 'Clark'}
View Factory function
const alien = {
sayHello () {
return `Hello, my name is ${ this.name }`;
}
};
const createAlien = (name) => {
return Object.assign(Object.create(alien), {
name
});
}
View Inheritance from object
var greenAlien = {
sayHello: function() {
return `Hello, my name is ${ this.name }`
}
}
var redAlien = Object.create(greenAlien);
redAlien.fly = function() {
console.log('I can fly');
}
View Object.assign
var A = { name: "a" };
var B = { dev: true};
//var C = Object.assign(A,B);
// taki zapis podmieni nam obiekt A
//dlatego, że pierwszy argument to źródło kopiowania wszystkich innych
//zatem zróbmy tak:
var C = Object.assign({}, A,B); // Obiekt C wygląda tak: {name: "a", dev: true}
View Object.create
var A = {};
var B = Object.create(A);
You can’t perform that action at this time.