Sunday, August 05, 2007

prototype javascript library Class example


<script type="text/javascript" charset="utf-8">
function doc (argument) {
document.write(argument);
document.write("<br />\n");
}
var Class = {
create: function() {
return function() {
this.initialize.apply(this, arguments);
}
}
}

var MyClass = Class.create();
MyClass.prototype = {
initialize: function (msg) {
this.msg = msg;
return msg;
},

showMsg: function () {
doc(this.msg);
}
}

var c = new MyClass('test'); // Class.create('test')
c.showMsg();

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

doc(o.info());
doc(o.ivk());

/*function TestThis () {
doc(this); // object Window
this.name = 'name test';
this.sex = 'name man';
}
var t = TestThis();
doc(name);
doc(self.name);
doc(this.sex);
for (var p in this) {
doc(this['p']);
}*/
</script>

The Global object holds the global properties and methods listed. These properties and methods do not need to be referenced or invoked through any other object. Any variables and functions you define in your own top-level code become properties of the Global object. The Global object has no name, but you can refer to it in top-level code (i.e. outside of methods) with the this keyword. In client-side JavaScript, the Window object serves as the Global object. It has quite a few additional properties and methods, and can be referred to as window.

No comments :