Skip to content

Instantly share code, notes, and snippets.

@kaaes
Created May 19, 2011 14:54
Show Gist options
  • Save kaaes/980930 to your computer and use it in GitHub Desktop.
Save kaaes/980930 to your computer and use it in GitHub Desktop.
Example of that/self workaround
var JSobj = function(id) {
var that = this;
var date = new Date();
that.id = 'JSnode_' + id;
that.element = document.getElementById(id);
that.createdAt = date.getTime();
that.element.addEventListener('click', clickCallback, false);
function clickCallback(evt) {
// 'this' is an object on which we call the function
// 'that' is always the reference to prototype object
// log id of the HTML element
console.log('HTML id -> ' + this.id);
// log id we've given to the JS object ('JSnode_HTMLid')
console.log('JS id -> ' + that.id);
// log time of creation
console.log('created at -> ' + that.createdAt);
}
}
var divEl = new JSobj('cont');
// it will log:
// HTML id -> cont
// JS id -> JSnode_cont
// created at -> 331305814994963
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment