Thursday, December 8, 2011

Json is Strict

I have been using JSON as DTO (Data transfer Object) for quite some time.
Today while educating myself with jquery came across an interesting thing about it.

So far today I knew the following is valid, but i was wrong
{ id : '01', name: 'kris' } <= invalid

It should be

{ id : "01", name: "kris" } <= valid


Refer : http://json.org/ for more information

Extjs accepts the above invalid data but jquery doesn't (as far as i have educated myself in jquery).

Aint that interesting !!!

Thursday, October 13, 2011

Lazy Evaluation


var boolV = true || false || true || false || true;
 console.log(boolV); //output is true

Question is did all the operations (logical) got executed ?
Ans is NO.

If several logical operations one after the other, but the result becomes clear at some point before the end of entire operation, then the remaining operations are not performed.

Lets consider the above statement.

var boolV = true || false || true || false || true;

It is before the first local operation it is clear that boolV is  suppose to have value true irrespective of any logical or operation. hence the other logical operations are not executed. in the above statement.

To verify this, we will the following code.

var a =10;
console.log(a); //output 10
console.log(!!++a); //output true
console.log(a);        //output 11
var boolV = true || false || true || false || true || !!++a;
console.log(boolV);  //output true
console.log(a);         //output 11 (which imples the !!++a didn't got executed)
boolV =  !!++a || true || false || true || false || true ;
console.log(boolV); //output true
console.log(a);        //output 12 (which imples the !!++a  got executed)


Ain't that interesting !!!

Wednesday, October 12, 2011

Starting reading Object Oriented Javascript by Stoyan Stefanov again

Starting reading Object Oriented Javascript by Stoyan Stefanov again.

I used to read this book managing between my office work and completing house hold choir.
Interestingly this time, I am being asked to complete this book by tomorrow (thats a big task) and that too as an work which I'm liking it.

This book contains conceptual treasures of javascript and everytime I read, I am starting to like it more.

Small interesting codes for all.

var a=10;
var b =20;
var c = "20";

var d,e,f,g;

d= a+b;
e= a+c;
f= b+a;
g= c+a;

console.log(d);
console.log(e);
console.log(f);
console.log(g);

Can you guess the output???
d>>> 30
e>>>  1020
f>>> 30
g>>> 2010

the value of d and f remains same whereas e and f are different. since e and f becomes string variable  from the operation  e= a+c; and g= c+a;

<< number >> + << string >> will result in string concatenation
and result type will also be << string >>.


Thursday, June 23, 2011

HTML5 & Detection Techniques

New Logo of  HTML5
HTML5 is the new specification of HTML 5 with lots of new feature and it is still being drafted the latest specification can be found at http://dev.w3.org/html5/spec/Overview.html .

Some of the new features are
  • Numerous new tags like (article, aside, bdo, command, datalist, details, embed, footer, hgroup, keygen, mark, meter, nav, output, progress, rp, rt, ruby, section, source, summary, time, wbr)
  • Custom tag for audio and video
  • Custom tags for images and its captions (figure, figcation)
  • Custom tag for Custom Graphics (canvas)
  • New controls for webforms like (dates, times, email, url, search, number, range, tel, color).
  • Geolocation Support.
  • Web Sockets: Socket programming for client browser.
  • New logo to identify  HTML5.
The are lots of more features in it, I am yet to go through all of them and the draft is still being updated.

Canvas element allows dynamic and scriptable rendering of 2D shapes and bitmap images. [Read More...]
SVG (scalable vector graphics is supported using canvas element). Various representation of reports are presented using graphs, pie charts, etc. canvas element with the help of SVG allows to create nice images and graphics. Javascript is required to program the SVG in canvas element.

How can I detect if the browser is supporting HTML5?
Here we have to depend on javascript. A javascript library named Modernizr available under Open Source for HTML5 and CSS3.
However, as per Mark Pilgrim('s book HTML5 Up and Running), we can employ the following simple techniques to quickly identify HTML5 compatible browser.

  1. Checking the presence of certain property on the global object(such as navigator or window object).
    • Example. checking the presence of Geolocation support.
    • function supports_geolocation() {
            return !!navigator.geolocation;
      }
  2. After creating an element dynamically, then testing the presence of a certain property to that element.
    • Example. checking the canvas support.
    • function supports_canvas() {
            return !!document.createElement('canvas').getContext;
      }
  3. After creating an element dynamically, then testing the presence of a certain method to that element. Then call the method if present  and check the value returned.
    • Example. checking the video support.

    • function supports_h264_baseline_video() {
            // checks the video support
            if (!!!document.createElement('video').canPlayType;) { return false; }
            var v = document.createElement("video"); //create element
            return v.canPlayType('video/mp4; codecs="avc1.42E01E, mp4a.40.2"');
      }
  4. After creating an element dynamically, set value to a property and then to test if it has retained the value.
    • Example: Create a element 'input' dynamically (which is of type 'text' as default). Then setting the property 'type' to other value and retesting the value of the property 'type' 
    • function supports_PropertyPersistance() {
         var i = document.createElement("input"); //default element is text
          i.setAttribute("type", "color");
          return i.type !== "text";
      }

Read More on detection technique...

Reference: http://en.wikipedia.org/wiki/HTML5
Specification - Working Draft: http://dev.w3.org/html5/spec/Overview.html
Specification - Released Working Draft: http://www.w3.org/TR/html5/

Monday, June 20, 2011

Rule of Thumb : Declaring variables

"var" is the keyword used for declaring a variable. It lets you define an number, string , datetime value or an object or function.


Example(s):

var a1;
var a2 = 10;
var a3 = 1.23;
var a4 = "hello world";
var a5 = true;
var a6 = {prop1:'some values', prop2: 'some other values', prop3 : true} ;


var a7 = new Object();
var fn1 = function() { return "hello world"};


what happens if we use in the following way
 ax=10;

Well well, the above line adds a variable name "ax" in the global scope. At a glance it can pretend to be an trivial mistake, but that is not the case, rather it is of very high importance, since such statement can make the global scope dirty and making the code fault prone and fragile.

Lets take another example for more clarity:


function sayHello(username){
  strn = "Hello " + username;
  return strn;
};


console.log( sayHello( 'kris' ) );
console.log( strn ); //=> "hello kris"

*console is a class available in almost all browsers but not in all browser for development/debugging purpose.

The above javascript function "sayHello" adds a variable "strn" to the global scope.

The following change of declaring the variable with "var" will make sure that the variable "strn" is applicable to local scope only.

Examples: 
function sayHello(username){
  var strn = "Hello " + username;
  return strn;
};


console.log( sayHello( 'kris' ) );
console.log( strn ); //=> undefined
Hence the scope of  local variable is managed correctly.

So we conclude this post with the memoirs of golden rule of variable declaration in javascript.

Tuesday, June 14, 2011

Beginning Javascript

The blog of mine is making a focus shift from blogging to javascript preaching.

JavaScript was earlier called "LiveScript".
you will get a good information on javascript history here wikipedia:javascript

My inception of javascript was like simple validations for web pages in 2005. Beginning 2007, I understood that javascript has some more potential. Errie, whats the potential. Well well, you can use javascript as a full blown applications. During june-2008 I wrote my first javascript class. Yessss my first javascript class.

well here it goes.

Code:



var A= function(){
  this.sayHello = function(){
    alert('hello world');
  };
};
var o = new A();
o.sayHello();


Yes it is as simple as written here.

More is on way to you.

Wednesday, April 6, 2011

ExtJS new version

Exciting new Sencha (EXTJS version4) has arrived.

Sencha is formerly known as EXTJS(EXTended JavaScript).

The interesting question is "So what exciting about it????"

The following are to name a few.

Using a swf runtime super cool "Drawing & Charts" exploiting the advantaqge to positioning the SWF component inside DOM using javascript with possibility to pass parameters. Its capability is not restricted rather increased with the ability of scalable vector drawing.

The Ext.Data package has been overhauled with interesting additions.

Its not last but the UI which matters the most is now supported by CSS3 themes, and the theme has been overhauled to give more richness with reduced size on browser.

Last but not the lease the "Performance" aspect has been improved drastically.
The reduced size load at DOM(i.e. DOM reduction) with an implied and tuned effect on speed gains. And the most interesting is the Infinite Scroll on Grids with data request on demand to scroll.

Click [here] to check out the beta4 version of Sencha .
There was an error in this gadget