Sunday, August 05, 2007

prototype javascript library Object extend example


<script type="text/javascript" charset="utf-8">
function doc (argument) {
document.write(argument);
document.write("<br /><br />\n");
}

Object.extend = function(destination, source) {
for (var property in source) {
destination[property] = source[property];
}
return destination;
}

var o = {
test: 'test it',
sex: 'man',
info: function () {
return this.test + " and " + this.sex;
},
ivk: function () {
return this.info.apply(this); // invoke this.info function
}
}

for(var p in Object) {
doc('Object["' + p + '"] = ' + Object[p]);
}

doc("=============================================================");

Object.extend(Object, o);

for(var p in Object) {
doc('Object["' + p + '"] = ' + Object[p]);
}

alert(Object.ivk());

</script>

No comments :