आप एक फ़ंक्शन बना सकते हैं जो एक तर्क संख्या को ले जाता है:
function setAttributes(elem ) {
for (var i = 1; i < arguments.length; i+=2) {
elem.setAttribute(arguments[i], arguments[i+1]);
}
}
setAttributes(elem,
"src", "http://example.com/something.jpeg",
"height", "100%",
"width", "100%");
या, आप किसी ऑब्जेक्ट पर विशेषता / मान जोड़े पास करते हैं:
function setAttributes(elem, obj) {
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
elem[prop] = obj[prop];
}
}
}
setAttributes(elem, {
src: "http://example.com/something.jpeg",
height: "100%",
width: "100%"
});
आप अपनी स्वयं की श्रृंखला योग्य वस्तु आवरण / विधि भी बना सकते हैं:
function $$(elem) {
return(new $$.init(elem));
}
$$.init = function(elem) {
if (typeof elem === "string") {
elem = document.getElementById(elem);
}
this.elem = elem;
}
$$.init.prototype = {
set: function(prop, value) {
this.elem[prop] = value;
return(this);
}
};
$$(elem).set("src", "http://example.com/something.jpeg").set("height", "100%").set("width", "100%");
काम करने का उदाहरण: http://jsfiddle.net/jfriend00/qncEz/
Object.assign()उन लोगों के लिए एक नज़र के लायक है जो एक सहायक फ़ंक्शन नहीं बनाना चाहते हैं - "सभी असंख्य और स्वयं के गुणों" के लिए काम करता है ।