Warning: file_get_contents(/home/mprokes/public_html/mattprokes.com/wp-content/themes/kanata/images/submiit.gif): failed to open stream: No such file or directory in /var/www/wordpress/wp-content/themes/kanata/header.php on line 39

Archive for the ‘New Ideas’ Category

Over the past few years I have worked on several javascript projects, many of which use and require a class paradigm of some sort (Open API’s). So to say the least, this is an experience which is irritating for new people and is still irritating for experienced programmers. The complexity of the problem is due mostly to the fact that there are 3-4 ways to do some things in the language (this is one of them), and none of the ways to do it really is right in any traditional sense of the word.

So for the JOY of throwing another wrench in the basket, I bring you yet ANOTHER way of doing namespaces in javascript programming, but don’t worry this should be a very pleasant experience.

First let me start off with how the problem began,

  • One day matt was asked to program yet another open API, and on this day matt was sad.
  • The reason was because on a previous project matt had tried an object literal style of programming (the x = {}; style), of which he found private fields were not to be found, and the requirements of an object literal (for class programming) to be very limiting  with no constructor of any sort.
  • He also remembered the wonders of an even earlier project, which used traditional javascript OOP programming (new operator). This style offered solutions to many of problems related to defining classes using object literals, but yet had its own set of problems. One of particular irritation was a property of javascript which makes it very difficult to build namespaced objects (which is why I think we do namespaces in JSON objects these days).
  • Read about the two methods here: http://www.javascriptkit.com/javatutors/oopjs.shtml

Now being a programmer who is mandated to use jquery, he was a fish out of water. Since jquery is not a library that lends its self well to a multi-level namespace matt was forced to come up with a new method for programming namespaced API’s, regardless.. Should matt really have to rely on a library to define a namespace in javascript?? NO.

Thus I introduce to you, classical javascript OOP with namespaces. Which I would affectionately like to call “Javascript with Classes”.

Properties of javascript class programming.

  • All namespace objects are anonymous instantiated singletons. Thus carry all the properties of a typical object. This levels the playing field, there are no “special” object literals.
  • I have been using it for several months, it works on all browsers.
  • Since everything is instantiated first, objects in the namespace may easily share references, via this.
  • Namespace objects may not be instantiated, objects within the namespace may be instantiated, this method unifies classical javascript OOP and namespaces. You are able to control what objects may be instantiated with this method.
  • Namespace objects have all the standard things any other function has, including privates, this, prototype, etc.

Finally, this method of javascript programming isn’t really all that special. To be honest it was looking me right in the face for the longest time, but it wasn’t until now that I get why it is important. I would say that probably the nicest thing about this method is that it unifies namespaces and classical javascript OOP, I can throw out structuring classes in object literals all together, there are also several other advantages including much cleaner code.

So enough talk, lets cut to the chase:

(The red highlighted code is the winner, when looking at simplicity)

  • Lets create a namespace object called HelloWorld

Object Literal Way:

HelloWorld = {
   stuff:function(){
      //stuff
   }
};

Classes Way:

HelloWorld = new (function(){
   //stuff
})();

Classical Way:

function HelloWorld(){
   //stuff
}
  • Simple enough, lets now add another level to our namespace, say some animals.

Object Literal Way:
Notice: Whoa, where did function come from? Melding of 2 different concepts here. No concept of closures, you will see later how this can get extremely complicated, and even unsafe.

HelloWorld = {
   "Animals":{
      "Bunnie":function(){},
      "Fish":function(){},
      "Bear":function(){}
   }
}

Javascript Class Way:
Notice: things stay consistent. The anonymous singletons are the namespaces, functions are the objects. Think about how closures come into play with this method.

HelloWorld = new (function(){
   var hw = this;
   hw.Animals = new (function(){
      var animal = this;
      animal.Bunnie = function(){};
      animal.Fish = function(){};
      animal.Bear = function(){};
   })();
})();

Classical Way:
Notice:You can see where classical starts to break down with namespaces, you need to declare every level of the object. Highly irritating, if you have several levels. Everything is declared out of scope, so you will see later how closures factor into things. Also, unless you want your user to be able to instantiate HelloWorld on the fly, this is not a standard namespace.
This is not really even correct in many ways..

HelloWorld = function(){}
HelloWorld.Animals = function(){};
HelloWorld.Animals.Bunnie = function(){};
HelloWorld.Animals.Fish = function(){};
HelloWorld.Animals.Bear = function(){};
  • Excellent, now lets say that we wanted one function in the object domain to access another.

Object Literal Way:
Notice: Humm, notice how we had to type in the full object path here, this is one of the downfalls of object literals, in the fact that scope is not retained in the literal. This becomes highly irritating if  you at all try to create any sort of complexed javascript object model.

HelloWorld = {
   Animals:{
      Bunnie:function(){alert('Bunny');},
      Fish:function(){alert('Fish');},
      Bear:function(){
         alert('A Bear Has A');
         HelloWorld.Animals.Fish();
      }
   }
}

Javascript Class Way:
Notice: Humm, this is nice, notice the fact that since everything is in a closure, we are able to retain scope. We don’t have to work down the heirarchy, just use the closes parent object to reference sub-objects. If you do lots of api calls which is what about 30% of code is, this can greatly enhance how clean code is.

HelloWorld = new (function(){
   var hw = this;
   hw.Animals = new (function(){
      var animal = this;
      animal.Bunnie = function(){alert('Bunny');};
      animal.Fish = function(){alert('Fish');};
      animal.Bear = function(){
         alert('A Bear Has A');
         animal.Fish();
      };
   })();
})();

Classical Way:
Notice:Really, classical is getting worse here, and we have the same problem that the object literal has, as the fact that there is no retention of scope.

HelloWorld = function(){}
HelloWorld.Animals = function(){};
HelloWorld.Animals.Bunnie = function(){alert('Bunnie');};
HelloWorld.Animals.Fish = function(){alert('Fish');};
HelloWorld.Animals.Bear = function(){
   alert('A Bear Has A');
   HelloWorld.Animals.Fish();
};
  • Lets say that we wanted to create a private field in a domain object.

Object Literal Way:
Its not possible to do it.

Javascript Class Way:
Due to the fact that sub classes are defined in the parent class scope is inherent, and privates become easy to achieve. Privates only exist in the confines of a { and }, so a parent object could never access a child private.

HelloWorld = new (function(){
   var hw = this;
   var privateHello = 'Hello World';
   hw.Animals = new (function(){
      var animal = this;
      animal.Bunnie = function(){alert('Bunny');};
      animal.Fish = function(){alert('Fish');};
      animal.Bear = function(){
         alert('A Bear Has A');
         animal.Fish();
         alert(privateHello);
      };
   })();
})();

Classical Way:
You can define a private variable in the HelloWorld Object, but due to scope issues you can’t share it with any other object.. *sigh*.

HelloWorld = function(){var privateHello = 'Hello World';}
HelloWorld.Animals = function(){};
HelloWorld.Animals.Bunnie = function(){alert('Bunnie');};
HelloWorld.Animals.Fish = function(){alert('Fish');};
HelloWorld.Animals.Bear = function(){
   alert('A Bear Has A');
   HelloWorld.Animals.Fish();
};

Hummm whenever we start to do anything complexed, a clear winner is beginning to emerge here. Lets continue, and try some other common programming practices. How about we try out constructors. … More on this a bit later.. getting late…


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

Audio and Video Trends

Author: mprokes

Alpha Edition!, e-mail spelling/grammer/topic suggestions to mattprokes@gmail.com

previous1next1The Professional Developer Series
Volume 2, Web Development

Chapter 1.4 Web Development Trends

Page: #25



Audio and video content are just some of the latest additions to the web development world, and web based media players have existed as recently as 2002. Some of the current limitations of media is the fact that there is not yet any standardized audio and video protocol built into the browser, although recently a video player was built with javascript (but it has no sound). Meaning that these technologies can only be leveraged through third party plugins like flash, java applets, quicktime, and realplayer

Audio:
Audio is also a continuing trend on the net, I expect that to continue, most audio players are based on flash based mp3 players these days, and you often see audo used on social networking sites particularly myspace. I will also expect to see crowd sourcing efforts with audio in the future, but I won’t clutter this book on what that might look like. Html 5 is also mandating native support for non-synthasized audio in the future.

Native Music Formats:
There is a couple natively supported audio formats in the browser; one is called .midi, although .midi media is not very good since it is an “instrumental” based media format, midi is basically synthesized music. The music  generated and played from the format that is similar to a sheet of music. Thus you can’t actually “record” music and play it back in .midi, for example you could never listen to the latest metallica recording through the midi format.

If I were to compare midi to an mp3, its a bit like comparing clip art to a picture of a person. They are both images, but one is very realistic, and the other is a drawing and not so realistic. Midi music usually sounds like the keyboard music heard back in the 1980′s, and it doesn’t always sound the same computer to computer, since it is synthasized. One advantage of midi is that a 4 minute song can usually be crammed into 70k to 80k, versus a mp3 which would be a solid 4MB to 5MB. Midi, can be leveraged to add short little sounds to you web application, and it is very efficient, and relevant in that case.

The second supported format is .wav, the wav format is similar to the one used with cd-roms (cdma) that being .wav is a loss-less format meaning that .wav audio is an exact binary copy of audio (with no compression loss, as mp3′s have). While being a lossless format is great from a quality of sound perspective, compression not being available for wav format is huge issue because a wav file can consume as much as 10MB just for 1 minute of audio. Compared to mp3′s, wavs take 10-11 times as much bandwidth and disk space, thus wavs are not really used to much in internet media because of this very issue.

Video:
Video was originally leverage (as so many things are) by porn sites, but later many popular video sites started springing up all over some of the best ones are youtube, internet archive video, hulu, and hundreds more, including many that are built into blogging software like wordpress. I expect videos to become an even more populat trend on the internet. Infact the next big fronteer for video will probably be intelligently integrating and tagging, and commenting on top of the video in real-time. This will allow search engines to begin searching video based on what people tagged and commented on it. Nothing like this exists yet, but here is a simple example.

videocomment2

While we are starting to see tagging like this on photos I expect to see comments also strapped to video and audio timelines in the near future. This will also open up a whole new world for things like video tutorials, etc.. all this can be done outside the video leveraging javascript, and then the data generated by users index and made searchable. Later in the book I may break out a javascript project which does exactly this. Crowd sourcing to the collaboration of a community to create content, and is a perfect example of crowd sourcing.  Html 5 mandates native video support for browsers, the jury is still out on the official codec that will be used although word on the street is it will be ogg/vorbis.

Media, and Bandwidth
If you decide to travel down the path of media, something you should be aware of is the intense amounts of bandwidth that it consumes, while media is excellent for a user experience at this time its bandwidth cost/profit ratio is not so good. Although you can often offset the bandwidth costs by uploading content to free services like youtube, and internet archive. Keep in mind though if you do this, the content service often mandates an open/free copyright of content on their system.

So really it is all about control of the content, if you are just planning on giving your content away, then upload it to a free service all you like. Although, if you absolutely need to retain copyrights, and don’t want to fumble around with all the legal issues associated with protecting your content on a free service, then you will need to host the content yourself and take a hit with bandwidth costs.

I expect that eventually a client technology will come along that will allow browsers to effectively make themselves a sort-of, bit-torrent-like type of technology which will allow for streamlined distribution of all web content including video. Several things need to happen though for that to become a reality, so you probably should not expect it to happen anytime soon (next 10-15 years).


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