CSTrüter HomeArticlesDownloadsAbout meContact me
How to enable the filestream feature in SQL 2008 - Alternative way to store blobs(files) via SQL 2010-08-21 19:31:56
How to create a Singleton Pattern in C# 2010-08-10 22:52:52
How to prevent that threads access shared resources concurrently via Monitor. 2010-08-06 15:31:15
A quick review of the book PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide written by Larry Ullman 2010-08-04 21:48:58
How to prevent that threads access shared resources concurrently via Mutex. 2010-08-03 14:42:36
How to stop propagation of javascript events 2010-07-25 21:59:29
Post about how Pete the web developer fixed his sitemap 2010-07-17 15:12:02
How to setup an out of process session service 2010-07-08 17:51:46
How to display/add images from/to a SQL Database 2010-07-04 23:15:15
How to register a custom URL protocol handler 2010-06-28 20:34:01
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
Populate a TreeView control in a windows application. 2009-08-27 16:01:03
2007-02-22 12:00:00
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();
nice ^^ thanks!
The company I am currently working for as software developer.
Collection of C# snippets 2010-05-22 01:06:19
Collection of MS SQL snippets 2010-05-22 00:55:15
Collection of JavaScript snippets 2010-05-22 00:37:57
Collection of ASP.net snippets 2010-05-22 00:29:56
Collection of PHP snippets 2010-05-22 00:06:45
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