Thursday, July 12, 2007

Prototype of Javascript


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

function Google (name) {
// constructor body...
var url = 'http://www.google.com';
this.url = url;
this.name = name;
}

Google.prototype.desc = "this is google.";
Google.prototype.title = "GOOGLE";
Google.prototype.toString = function () {
return "Object [Google].";
}

var g = new Google('yu');
doc('g=' + g);
g.test = 'test';
doc("g.test=" + g.test);
doc("g['test']=" + g['test']);
doc("g.url=" + g.url);

g = null;

function ExtGoogle (argument) {
this.ext = argument;
}

ExtGoogle.prototype = new Google('yu'); // before toString and ajax function.
ExtGoogle.prototype.toString = function () { return "Object [ExtGoogle]."};
ExtGoogle.prototype.ajax = function (url) {return 'Ajax function.'}

doc('');
var e = new ExtGoogle('yu');
doc(e.ajax('Google'));
doc(e);
doc("e['ext']=" + e['ext']);
doc(e.url);
doc(e.title);

e = null;
</script>

No comments :