Skip to content

Instantly share code, notes, and snippets.

@InFog
Created April 25, 2013 11:12
Show Gist options
  • Save InFog/5459018 to your computer and use it in GitHub Desktop.
Save InFog/5459018 to your computer and use it in GitHub Desktop.
Exemplo de OO em JS usando uma função. Existe uma associação de evento para definir o nome de p1.
var MyClass = function () {
this.name = "";
this.setName = function (name) {
this.name = name; // Este this é o MyClass, não a function atual
};
this.getName = function () {
return this.name;
};
this.keyPressSetter = function (k) {
this.name = this.name + String.fromCharCode(k.keyCode);
console.log(this.name);
};
};
p1 = new MyClass();
p2 = new MyClass();
p2.setName("Jack");
document.getElementById('myText').onkeyup = function (k) {
p1.keyPressSetter(k);
};
console.log(p2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment