Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Thursday, July 14, 2011

javascript math

So what is 70.18*100 in javascript? It might be surprise you that the result is 7018.00000000001.

And number.toFixed() should fix this nicely.

Another interesting thing about jqGrid. I have a date column that requires a customized format but needs to be sorted as date. I did some research and none of the online solution works (my jqGrid version is 3.6.5). So I download the source, and what's going on is jqGrid parse the date with y,m,d,h,i,s A (as year, month, date, hour, minute, second, AM/PM). So the solution is:

sorttype: 'datetime', datefmt: 'm-d-y h:i:s A' (I am mm-dd-yyyy hh:mm:ss AM/PM format).

Wednesday, July 6, 2011

Cool jQuery UI

Haven't been doing scripts for a while, and there is one from msdn that looks pretty cool: http://msdn.microsoft.com/en-us/scriptjunkie/hh127352.aspx

Kind of surprised how easy it is doing round corner UIs :-)

Thursday, January 6, 2011

Some Javascript basics

I used javascript for many years, did many things that solved hard problems at work. However I recently changed work, and this company used javascript heavily.

Through my recent debugging and fixing of the code, I found some old javascript tricks that I never know!!

1. === and !==. "===", the triple equal sign, means compare with type. So even though 2=="2" is true, 2 === "2" is false because their types are different. Same with !==. I found this one while I was reading jQuery code.

2. function closures.
(function(){
//do something without affect globals.
})();

2. call and apply.
Suppose you have a function doSomething(a,b). You can call itwith a new "this" by using call and apply. i.e

3. arguments
It is a variable made available by javascript that contains current function call's arguments.

function doSomething(a,b){
alert(this.id + ' ' + a + b);
}
var obj = {id:'me'};
var func = doSomething;
func.call(obj, 1, 2);
(function(){func.apply(obj, arguments);})(1,2);

the above code will show "me 12" twice.

Because I never know those before, the first time I saw them, I thought they are either typos or some home made functions!!

Monday, March 16, 2009

In love with Lambda expressions

I wanted to do this for a while now, never find the time. So it is a short one.

I wanted javascript for zoloFighter for one simple reason. At the time I love java, and this Paul Graham's essay prompted me to look into python/ruby/javascript.

It is excellent that most of zoloFighter users uses javascript to customize their bots, turning boring grinds into fun programming exercises, which is beneficial to them in the long run (and as paranoid as I am, which constantly worry about employer's ability to pay employee, or US run out of engineers, it is a good thing to do). One huge thing I like about javascript is attach of functions to anything, or anonymous functions (which java also have but c# 1.0 lacks).

How things has changed since c# introduced Lambda expressions. Now you can do anonymous functions and much more! I love the cleanness of those, they just looks beautiful and make our job much easier.

Monday, April 14, 2008

javascript engine and thread safety

Well, I was doing some simple AJAX at work and was wondering how will the engine handle script safety.

I remember when I was debugging the javascript engine for zoloFighter, the engine appears to be single thread only.

Some search on the internet, I found this : http://odetocode.com/Blogs/scott/archive/2007/01/08/9734.aspx
The author seems to agree with me but still it is not clear.

So it is time to try it out! Sure enough, both IE and Firefox serialize everything on one thread only :-)