Skip to content

Instantly share code, notes, and snippets.

@juliozuppa
Last active April 20, 2017 14:11
Show Gist options
  • Save juliozuppa/9b7c5ac8c4d76eb301c2d15451965333 to your computer and use it in GitHub Desktop.
Save juliozuppa/9b7c5ac8c4d76eb301c2d15451965333 to your computer and use it in GitHub Desktop.
// INPUT EVENT
var texto = document.getElementById("texto");
// evento input é disparado depois de o texto ser inserido em um elemento (não é possível cancelar)
texto.addEventListener("input", function () {
this.value = this.value.toUpperCase();
});
// call - mudando o this do contexto
var obj = (function() {
var privateFn = function() {
console.log(this.id);
}
return {
id: 123,
publicFn: function() {
privateFn.call(this);
}
};
}());
obj.publicFn();
// usar a função de um objeto em outro objeto
// digamos "herdar" o método describe
var Robert = {
name: "Robert Rocha",
age: 12,
height: "5,1",
sex: "male",
describe: function() {
return "This is me " + this.name + " " + this.age + " " + this.height + " " + this.sex;
}
};
var Richard = {
name: "Richard Sash",
age: 25,
height: "6,4",
sex: "male",
}
console.log(Robert.describe.call(Richard)); // This is me Richard Sash 25 6,4 male
// foreach javascript
['a', 'b', 'c'].forEach(function (child, index) {
console.log(index + ': ' + child);
});
[].forEach.call(['a', 'b', 'c'], function(child, index, arr) {
console.log(index + ': ' + child);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment