Skip to content

Instantly share code, notes, and snippets.

@qetr1ck-op
qetr1ck-op / berlin-jsconf-2014.md
Created October 3, 2015 10:35 — forked from nikcorg/berlin-jsconf-2014.md
Slide decks of JSConf 2014
// 1. constructor Animal
function Animal(name) {
this.name = name;
this.speed = 0;
}
// 1.1. prototype methods
Animal.prototype.stop = function() {
this.speed = 0;
alert( this.name + ' stops' );
function Animal() { }
Animal.prototype = {}; // override
var animal = new Animal();
console.log( animal.constructor == Animal ); // false
console.log( animal.constructor == Object ); // true
function Animal() { };
var animal = new Animal();
/*
creates automatically
Animal.prototype = {
constructor: Animal
}
*/
// 1. constructor
function Animal(name) {
this.name = name;
this.speed = 0;
}
// 2. methods in prototype
Animal.prototype.run = function(speed) {
this.speed += speed;
console.log(`${this.name} runnig, speed is ${this.speed}`);
/*
1. add defer() to all function prototypes
2. should wait for n second, then invoke a function
3. (!) and return a function
function f() {
console.log( a + b );
}
f.defer(1000)(1, 2); //after delay 1sec, 3
*/
Function.prototype.defer = function(ms) {
/*
1. add defer() to all function prototypes
2. should wait for n second, then invoke a function
function f() {
console.log( 'Hello' );
}
f.defer(1000); //after delay 1sec 'Hello'
*/
/*
1. create prototype object
2. should itterate through object, aka forEach only for objects
obj.each(function(prop, val) {
console.log( prop ); // name -> age
});
*/
Object.prototype.each = function(f) {
/*
1. create method in string prototype
2. it should return multiple repeated string by n times
consle.log( "foo".repeat(3) ); // 'foofoofoo'
*/
String.prototype.repeat = function(times) { //or use built-in ES6 repeat()
return new Array(times + 1).join(this);
};
var arr = [1, 2, 3];
console.log(arr) // 1,2,3, result of arr.__proto__ -> Array.prototype.toString