CSTrüter HomeArticlesDownloadsAbout meContact me
a quick look at how to create a windows service using C# 2010-02-28 21:48:06
How to call server-side code from client-side code, using PageMethods in ASP.net 2010-02-21 12:31:27
How to pass a set of data to an xml type in SQL 2005/8 2010-02-12 19:04:23
Some funky behaviour regarding overload Resolution of dynamic/object types 2010-02-09 17:16:52
Object orientated programming within JavaScript 2010-01-28 07:25:45
How to sort data using ASP.net (C#) and SQL 2005/8 2010-01-18 15:23:14
Quick look at some of the new features added to C# 4.0 2010-01-12 21:52:13
SQL 2008 introduced a nifty feature called Table-Valued Parameters (TVP) into its codebase 2010-01-06 22:58:25
How to page data using ASP.net (C#) and SQL 2005/8 2009-10-19 15:01:45
a post about sql joins 2009-09-20 15:50:57
Creating a WYSIWYG textbox for your website is actually quite simple. 2007-02-01 12:00:00
Move items between two listboxes in ASP.net(C#, VB.NET) and PHP 2008-06-12 17:07:43
Firefox word wrapping issues 2008-06-09 09:51:21
2007-02-22 12:00:00
Blog about passing parameters by reference to functions using func_get_arg(s) 2008-07-27 12:38:24
JavaScript is an object-orientated language, some even use the term prototype-orientated language. In this post I am going to have a quick look at OOP within the JavaScript realm, and have a look at prototyping. In languages like C# & PHP, we create objects from classes/structs etc, within JavaScript however, we need to use functions to define our objects, observe: /*constructor*/ function base(name) { /*private*/ var title = 'base object:'; /*public*/this.name = name; /*global*/value = 10; this.display = /*public*/function(message) { alert(title + message); } this.toString = /*public*/function() { return "base"; } } var a = new base('test1'); // instantiated object a.display(a); // base object:base alert(a.name); // base object: alert(a.title); // undefined alert(value); // 10 Notice my comments, I gave certain functions fictitious access modifiers (eg private/public) to give you an idea of scope visibility of these members. The title property can only be accessed within the instantiated object (like a private member), the this.name field is publically available using the object. Notice the value field, if we omit the var keyword, it becomes a global variable - one can access it anywhere within the scope of our page. Constructor? Well, the whole function essentially represents our constructor. Another term used in OOP is "inheritance", which relates to the fact that we can add certain methods/properties etc from another class (function in this case), to our class(function) in question. In JavaScript this is achieved using prototypes, observe: /*constructor*/ function derived() { /*private*/function getDate() { return new Date(); } this.showdate = /*public*/function() { this.display(getDate()); } } derived.prototype = /*inherit*/new base('test2'); derived.prototype.toString = /*overrides*/function() { return "derived"; } var b = new derived(); b.showdate(); b.display(b); alert(b.name); Inheritance is achieved by assigning an instance of the function we wish to "inherit" from, to our prototype - which copies the members within that instance to our derived object. It is even possible to override inherited members, using prototypes, eg. the toString method. Prototypes also allow us to add functionality to existing built-in objects, similar to extension methods, in the following snippet we "alias" the addEventListener method in Mozilla to the attachEvent method - by "extending" the built-in Object. if (!window.attachEvent) { Object.prototype.attachEvent = function(event, handler) { this.addEventListener(event.substring(2), handler, false); } } Note : Have a look at the "private" getDate function within our derived function, if you attempt to access the instance you're working with (using the this keyword), you will notice that you actually get an instance of the current window object and not the actual instance it "lives" in - rather dodgy? Which means getDate isn't really a private method in the true sense of the word. Its also possible to create objects using javascript object notation (JSON), these objects unfortunately lack constructors, meaning we can't create/reuse new instances, since we're actually defining an inline object. var a = { display : function(message) { alert(message); }, toString : function() { return 'a'; } }; Unless you're planning to return the object from a "constructor" function...(mmmm???) function base() { var a = { // Object definition }; return a; } Finally I noticed on a few sites people asking about creating static methods, we can achieve something similar by doing the following: derived.somefunction = /*static*/function() { alert('somefunction called'); } derived.somefunction();
/*constructor*/ function base(name) { /*private*/ var title = 'base object:'; /*public*/this.name = name; /*global*/value = 10; this.display = /*public*/function(message) { alert(title + message); } this.toString = /*public*/function() { return "base"; } } var a = new base('test1'); // instantiated object a.display(a); // base object:base alert(a.name); // base object: alert(a.title); // undefined alert(value); // 10
/*constructor*/ function derived() { /*private*/function getDate() { return new Date(); } this.showdate = /*public*/function() { this.display(getDate()); } } derived.prototype = /*inherit*/new base('test2'); derived.prototype.toString = /*overrides*/function() { return "derived"; } var b = new derived(); b.showdate(); b.display(b); alert(b.name);
if (!window.attachEvent) { Object.prototype.attachEvent = function(event, handler) { this.addEventListener(event.substring(2), handler, false); } }
var a = { display : function(message) { alert(message); }, toString : function() { return 'a'; } };
function base() { var a = { // Object definition }; return a; }
derived.somefunction = /*static*/function() { alert('somefunction called'); } derived.somefunction();
Codebooth my semi-community site etc (work in progress)
The company I'am currently working for as software developer.
a Parallel reference of programming languages 2009-09-10 12:48:23
a tutorial explaining how to develop a simple login using PHP and MySQL 2009-09-05 18:26:47
An article looking at adding some kind of event driven model to PHP 5 2008-07-28 12:48:09
It is very simple creating your own rss reader, the following article looks at a few methods of doing this. 2008-06-23 13:18:25
A quick reference about working with dropdown boxes (select element) in javascript. 2007-02-17 16:36:41
Collection of funny programming articles 2006-10-08 14:23:43