See https://gist.github.com/1466219 for more details
-
-
Save thingsinjars/1466253 to your computer and use it in GitHub Desktop.
Chainable property setting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function to add properties in a chainable manner either as single strings or as an object | |
// usage: | |
// object.prop('propertyName', propertyValue) | |
// or: | |
// object.prop({'propertyNameOne':'propertyValueOne','propertyNameTwo':'propertyValueTwo'}) | |
function( | |
a, // Constructor | |
b, // Placeholder | |
c){ // Placeholder | |
a.prototype.prop=function(){ // Add the 'prop' function to everything passed through here | |
b=arguments; // Shorthand assignment | |
if(1 in b) // If arguments.length > 1, we've got a single property assignment | |
this[b[0]]=b[1]; // Set the property's name and value | |
else { // Else | |
b=b[0]; // We've been given an object of properties to assign | |
for(c in b) // For each one | |
this[c]=b[c] // Set the property's name and value | |
} | |
return this // The chainable bit | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(a,b,c){a.prototype.prop=function(){b=arguments;if(1 in b)this[b[0]]=b[1];else{b=b[0];for(c in b)this[c]=b[c]}return this}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
Version 2, December 2004 | |
Copyright (C) 2011 Simon Madine <http://thingsinjars.com> | |
Everyone is permitted to copy and distribute verbatim or modified | |
copies of this license document, and changing it is allowed as long | |
as the name is changed. | |
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE | |
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION | |
0. You just DO WHAT THE FUCK YOU WANT TO. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "propertize", | |
"description": "This allows properties to be added to an object in a chainable manner", | |
"keywords": [ | |
"DOM", | |
"properties", | |
"chaining" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<title>Chain anything</title> | |
<div>Expected value: <b><a href="http://140byt.es/" title="tiny awsm" data-monkeys="∞">140byt.es</a></b></div> | |
<div>Actual value: <b id="ret"></b></div> | |
<script> | |
var propertize = function(a,b,c){a.prototype.prop=function(){b=arguments;if(1 in b)this[b[0]]=b[1];else{b=b[0];for(c in b)this[c]=b[c]}return this}} | |
// see https://gist.github.com/gists/1466219 (enable DOM manipulation chaining) | |
var chainify = function(a,b,c){for(a in b=a.prototype)with({d:b[a]})d.call?b[a]=function(){return(c=d.apply(this,arguments))===[]._?this:c}:0} | |
chainify(window.Element); | |
propertize(window.Element); | |
document.getElementById('ret').appendChild( | |
document.createElement('a') | |
.prop({ | |
'href': 'http://140byt.es/', | |
'title': 'tiny awsm' | |
}) | |
.prop('innerHTML', '140byt.es') | |
.setAttribute('data-monkeys', '∞') | |
.addEventListener('click', function(){ alert('Urk! Alert!')}, false) | |
); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This works best when paired with the chainableDom mentioned in the test file.
The two of them together enable the full syntax used at the bottom of the test file.