/*
Testing that the function returned from bind is rereferenceable,
such that it can be added and removed as an event listener.
*/
function MyImportantCalloutToYou(message, otherMessage) {
this.message = message;
this.otherMessage = otherMessage;
this.original = true;
// the following is necessary as calling bind again does not return the same function
// so we replace the original function with the one bound to this instance
this.swap = this.swap.bind(this);
this.element = document.createElement('div');
this.element.setAttribute('class', 'callout');
this.element.addEventListener('click', this.swap, false);
this.element.appendChild(document.createTextNode(message));
document.body.appendChild(this.element);
}
MyImportantCalloutToYou.prototype = {
message: null,
otherMessage: null,
original: true,
element: null,
swap: function() {
this.element.firstChild.nodeValue = this.original ? this.otherMessage : this.message;
this.original = !this.original;
// now this function can be properly removed as an event listener
this.element.removeEventListener('click', this.swap, false);
}
}
var callout1 = new MyImportantCalloutToYou(
'Swapping of messages should only be possible once.',
'This is the swapped out message in the first callout.'
);
var callout2 = new MyImportantCalloutToYou(
'Swapping of messages should only be possible once.',
'This is the swapped out message in the second callout.'
);
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!