Archive for November, 2008

So today I am reading through this book pro-javascript design patterns, excellent book by the way! I had come upon chapter 2, which was an excellent explanation of interfaces. One thing that concerned myself is that the book states that it is impossible to create enforceable interfaces in javascript. I pondered that for a bit, as far as what interfaces really are. If you look at interfaces from the perspective of java, then for the most part they are types, with a specified subset of methods for a class instance.

Thinking in those terms, I sat back and pondered the problem again, sooo all I need to do really to create interfaces that are enforceable in javascript would be to separate the concept of an interface from a class all together and make it a class its self! WALA! True first class interfaces in javascript!

UML Diagram

So lets see what this new found idea looks like! Here is my interface class that makes it all work.

Interface.js

You can think of interface as linking everything together, but what would a TestInterface look like?

TestInterface.js

Take note, that the interface its self is already implementing Interface.js in a sort-of way! There are two required methods getInterfaceName() and setUpInterface() that need to be implemented otherwise Interface.js will complain loudly!

The basic idea here is that setUpInterface recieves a closure, that is able to access the implementing object, all the TestInterface needs to do is call that method with the same attributes it recieves. It does this by passing in the name of the method into the closure, and then executing the returned function with the same attributes recieved by the method; and return it, SIMPLE!

You can see this happening here:

return inspector(‘testMethod’)(x,y,z);

So now that we have an interface for our class, what does a javascript class have to do to implement this interface?

TestObject.js

The actual implementation of the interface happens here: new Interface(this, new TestInterface());
Wow! thats easy! But what happens when the class doesn’t implement all the methods as this one does?

Well lets find out, if you could place all these scripts in a javascript shell, lets test out a few things.

//Create a new TestObject();
TestThis = new TestObject();

//Lets cast our object to a type of ‘TestInterface’
CastedTestInterface = TestThis.cast(‘TestInterface’);

Don’t take my word for it, here is a FULL WORKING EXAMPLE!

Now if you inspect CastedTestInterface you may notice that it is just an instance of the interface you created! Keep in mind though that it also holds a closure to the object it was casted from.

Lets try executing a method:

CastedTestInterface.testMethod(1,2,3);

Whoa! it worked!
Lets try executing another method specified by the interface:

CastedTestInterface.testMethod2(1,2,3);

Opps, we get an error because this method does not exist! We better fix that, humm but this means that the interface is working!

Long story short, if a method you call from a cast is missing from the object; you will be alerted when you call the method, an error will be thrown, and you scolded for doing such a horrible thing! To be honest, this method is 1/2 of an abstracted class and 1/2 of an interface, an {interstraction}?!?!

This is because most interfaces specify that they can not have implementation details in them, but ours do. On the other hand this is not a abstracted class! Because abstracted classes are extensions of the original, when clearly this type of OOP not an extension, but a subset. Hope you enjoyed the tutorial on how to create interfaces :-) .

  • Reddit
  • StumbleUpon
  • Bebo
  • Yahoo Buzz
  • Delicious
  • Twitter
  • LiveJournal
  • Netlog
  • HelloTxt
  • Share/Bookmark

Commenting Your Code
Comments will always be an important part of programming; they are often used to describe whats going on in your code, and even for automatically generating documentation.

There are two styles of comments:

  • //Single Line

  • /*
    Multi
    Line
    */

Some issues with multi-line comments:

Since multi-line comments end with */ this means that you may never include a */ within the body of the comment.

example:

/*An error is caused when I use a */ as you can see.*/

Reserved Words
Reserved Words are used by the javascript programming language to identify programming structures, and execute instructions based on those structures.

There are 59 reserved words identified by the language as follows:

abstract, boolean, break, byte, case, catch, char, class, const, continue, debugger, default, delete, do, double, else, enum, export, extends, false, final, finally, float, for, function, goto, if, implements, import, in, instanceof, int, interface, long, native, new, null, package, private, protected, public, return, short, static, super, switch, synchronized, this, throw, throws, transient, true, try, typeof, var, volatile, void, while, with

I am also including (undefined, NaN, and Infinity) as being reserved, though they serve a different purpose of describing data rather than structure.

Some Notes On Reserved Words:

  • Not all reserved words are used by the langauge, so you will only need to memorize the functionality of a small subset.
  • Do not use reserved words to name object literals, properties, variables, parameters, or any other programming structure for that matter.

Numbers
Numerical values are and will continue to be used throughout your development career. Thus I suggest that you become familiar with the properties of javascript numerical values.

Numbers Are:

  • Wrapped up as objects when inspection is required, but are treated as primitives!
  • 64bit
  • Represented by a single object type (meaning an integer value of 1 is also 1.0 and a boolean value of true).
  • Covers classic number types such as int, float, boolean, double, and long

Integers are represented by a numerical value, but are not followed by a decimal place.

  • 123

Decimals are represented by numerical values containing a decimal place.

  • 123.456

Exponents are represented by a numerical value followed by an e and a positive or negative decimal placing.

  • 123e-3 (represents .123)

Negative numbers are represented with a – followed by a numerical value.

  • -123

Infinity is represented by numbers greater than 1.79769313486231570e+308

Strings
Strings represent data specifying a set of characters. Strings are also the most commonly used data for most languages. Javascript is no exception, whether creating javascript object literals or validating form data you will find that strings pose an important role in your programming career. Strings are objects and contain important functions for manipulation and objects details.

Strings can be specified through two methods:

  • “This is a string!”
  • ‘This is a string!’

Why two ways of writing strings?
This is because at the time javascript was created, it was not an uncommon perception that it may be integrated tightly with html. So if you have a tag that uses an onclick=”alert(“oops”)” this would escape the parameter before needed. This may be rectified  by escaping the quotes using \” but it is often more easy to just use a single ‘ as seen here onclick=”alert(‘yeey’)”. The duality of quoting often cancles its self out very nicely, so as to not impede programming.

Some other notes about strings:

  • Strings are immutable
  • Concatination is done with a + operator (ex: “hello “+”my “+”name “+”is “+”matt “)
  • Strings are objects
  • You may escape something within a string using a \ (ex “I want to see a \\ or a \” “)
  • Reddit
  • StumbleUpon
  • Bebo
  • Yahoo Buzz
  • Delicious
  • Twitter
  • LiveJournal
  • Netlog
  • HelloTxt
  • Share/Bookmark

Javascript

Author: mprokes

Javascript, the worlds most popular programming language is also the worlds most mis-understood language. The purpose behind these posts is to clear up somewhat the facilities of javascript, and make clear what this powerful language has to offer. I plan on providing clear examples, with no fluff. Enjoy.

  • Reddit
  • StumbleUpon
  • Bebo
  • Yahoo Buzz
  • Delicious
  • Twitter
  • LiveJournal
  • Netlog
  • HelloTxt
  • Share/Bookmark

I have been toying with a seemingly tidy distro recently called OpenGEU; and I have to admit, its one of the best distributions I have ever seen. Luna Crescente is a mix of ubuntu, enlightenment 17, and gtk based apps, it also uses stalone tray. The seemingly simplistic combination is one of the most beautiful lightweight linux desktops that I have seen in recent times (and i have tried them all!). It has the speed that enlightenment brings to the table, as well as the hardware/packaging support that makes ubuntu so great. Toss in a very tidy, and fresh looking orange gtk+ theme, and I would say that OpenGEU is by far the most beautiful distribution I have seen thus far out of the box.

In conjunction with its beauty, is the awesome firing squad of lightweight apps that I have come to appreciate. Among these are thunar (though I am usually a rox-filer fan I gave it a try and actually like it more!), many lightweight enlightenment based apps including pdf readers, music players, etc.. One of the great things about this distribution is that it actually believes in including not just the enlightenment desktop, but also the modules, which alone run very very fast! Other applications that I know and log in gnome are also included such as nm-applet, update managers, and synaptic to finish things off. Everything ran out of the box, and I specifically liked the live cd/install it try it before you buy it feature that is now very common in linux distributions. So I would suggest to all of you reading to go check it out! Download the live cd, and give it a spin I have had a very positive experience thus far.

  • Reddit
  • StumbleUpon
  • Bebo
  • Yahoo Buzz
  • Delicious
  • Twitter
  • LiveJournal
  • Netlog
  • HelloTxt
  • Share/Bookmark