A list of German bands
Clicking the button should show the band name
var bands = ['Apparat', 'Boy', 'Kraftklub'];
for (var i = 0; i < bands.length; i++) {
var band = bands[i],
button = document.createElement('button');
button.appendChild(document.createTextNode(band));
button.addEventListener('click', function(){
alert(band);
});
document.body.appendChild(button);
}
var bands = ['Apparat', 'Boy', 'Kraftklub'];
for (var i = 0; i < bands.length; i++) {
var band = bands[i],
button = document.createElement('button');
button.appendChild(document.createTextNode(band));
(function(band){
button.addEventListener('click', function(){
alert(band);
});
})(band);
document.body.appendChild(button);
}
['Apparat', 'Boy', 'Kraftklub'].forEach(function(band){
var button = document.createElement('button');
button.appendChild(document.createTextNode(band));
button.addEventListener('click', function(){
alert(band);
});
document.body.appendChild(button);
})
Object.prototype.oldStyleMethod = function oldStyleMethod (){};
var someObject = {};
for (var key in someObject) { console.log(key) };
This is why toString() doesn't appear in 'for' loops.
console.log(Object.prototype.toString);
function toString() { [native code] };
Requires native ES5 (not shimmable)
Object.defineProperty( Object.prototype, "newStyleMethod", {
value: function newStyleMethod(){},
enumerable: false
});
for (var key in someObject) { console.log(key) };
Past conflicts:
Set a sensible prefix
Underscore | Sugar | |
Methods | No | Yes |
Regular 'for' loops | Yes | No |
Conflict-free | Yes | No |
Underscore | Agave (ES5 only) | Sugar | |
Methods | No | Yes | Yes |
Regular 'for' loops | Yes | Yes | No |
Conflict-free | Yes | Yes | No |
var livebind = function(object, binding, properties){
properties.forEach(function(property){
var hiddenProperty = '_'+property
Object.defineProperty(object, property, {
get: function(){ return testData[hiddenProperty]; },
set: function(newValue){
testData[hiddenProperty] = newValue;
binding.set(property, newValue)
},
enumerable: true,
configurable: true
});
})
}