Skip to content

Instantly share code, notes, and snippets.

@thingsinjars
Forked from 140bytes/LICENSE.txt
Created December 12, 2011 09:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thingsinjars/1466253 to your computer and use it in GitHub Desktop.
Save thingsinjars/1466253 to your computer and use it in GitHub Desktop.
Chainable property setting
// 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
}
}
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}}
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.
{
"name": "propertize",
"description": "This allows properties to be added to an object in a chainable manner",
"keywords": [
"DOM",
"properties",
"chaining"
]
}
<!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>
@thingsinjars
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment