Skip to content

Instantly share code, notes, and snippets.

@wochap
Last active August 22, 2016 00:12
Show Gist options
  • Save wochap/1934470bbd038686678a to your computer and use it in GitHub Desktop.
Save wochap/1934470bbd038686678a to your computer and use it in GitHub Desktop.
Javascript docs

classes en js ECMAS5

Closure way

var Person = function (name) {
  var name = name
  
  function speak () {
    console.log('My name is ' + name)
  }
  
  var publicApi = {
    speak: speak
  }
  
  return publicApi
}

Prototype way

var Person = function (name) {
  this.name = name
}

Person.prototype.speak = function () {
  console.log('My name is ' + name)
  // be sure use `bind` if you use `this` inner a function
}

Inheritance (OLOO) (Objects Linked to Others Objects)

var Person = function (name) {
  this.name = name
}

Person.prototype.speak = function () {
  console.log('My name is ' + name)
  // be sure use `bind` if you use `this` inner a function
}

var Developer = function (name, area) {
  Person.call(this, name)
  this.area = area
}

Developer.prototype = Object.create(Person.prototype)

Developer.prototype.speak = function () {
  console.log('Hi ')
  Person.prototype.speak.call(this)
}
// if
if (A >= B) {
  alert('mensaje'); 
} else if (A <= B) {
  alert('mensaje'); 
} else {
  alert('mensaje'); 
}

// while
while (A == B) {
  A+=1
  alert('mensaje');
  if (condicion) {
    break;	//interrupte la interacion TODO
    // continue; //se detiene aqui y continua //ya no se ejecuta console.log
  }
  console.log(mensaje);
}

// for
for (var i = 0, len = 20; i < len; i++) {
  console.log(i);
  if (condicion) {
    break;	//interrupte la interacion TODO
    //continue; //se detiene aqui y continua //ya no se ejecuta console.log
  }
}

// switch
switch (condicion) {
  case 'A' :
    return 'mensaje' ; 
    break;
//retorna como valor y se puede guardar como variable luego		
  default :
    alert(mensaje);
    break;
}

// do while
do {
  sentencias;
} while (condicion);

// try
try {
  sentencias;
} catch (ExceptionClass e) {
  sentencias;
} finally {
  // this executes ever
}

Prototype props

// every
[].every(function (item) {
  // false to break loop, true for continue
  return true;
  return false;
});

var newArray = [].map(cb) // return item modified

var newArray = [].filter(cb) // return true for add item

var item = [].find(cb) // return true for add item

var newArray = [].sort(cb) // return -1, 1, 0

var str = 'cadena'
str.startsWith('')
str.endsWith('')
str.includes('')
str.repeat(2) // repite la cadena 
document.getElementById('caja')
document.getElementByTagName('div')
document.getElementByClassName('caja2')
foo = document.getElementById('foo')

// mouse
foo.onclick = function () {}
foo.onmouseover = function () {}
foo.onmouseout = function () {}
foo.onmousemove = function () {}

// navigate	
window.onload	= function () {} // cuando carga el elemento
window.onunload = function () {}	// cuando cierras el tab
window.onresize	= function () {} // cuando cambias el tamaño del navegador

// form
foo.onfocus = function () {} // selecciona campo
foo.onblur = function () {} // deselecciona campo
foo.onchange = function () {} // cambio valor de campo
foo.onsubmit = function () {} // cuando se envia un formulario
		
// tecla
foo.onkeydown = function () {}
foo.onkeyup = function () {}
foo.onkeypress = function () {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment