A while ago, a former classmate and current colleague of me phunehehe had a chance to work with a web app project that makes use of JS-OOP heavily. He was freaked out due to JS syntax and its way to do OOP. Yes, there are certain difficulties when moving from traditional OOP (Java, C#, etc...), but once you know the tips, it isnt something indigestible any longer :)
Basically, Javascript does not support a real class but it does allow you to create an object via a function. That special function is called constructor. With the keyword new, JavaScript makes the function become a constructor and instantiate a new object with the members defined by that function. Inside a constructor, the keyword this references the new object that's being created. It is also a convention that the name of the constructor is capitalized.
And nice enough, you can easily extend the function set of all objects created by a contructor using the prototype attribute.
How about a function that defined in both constructor function and constructor prototype? In that case the one that is defined constructor function will be used.
And from Douglas Crockford (Javascript: the good parts), we can easily implement inheritance with the combination of constructor function and prototype!
Finally, Singleton would be a good open to enclose this small talk about JS OOP. In short, Singleton is a class that has no public constructor but a special function that will return an instance of the class (and create one if the instance doesnt exist) Given Javascript nature, Singleton isnt a class, it also shouldnt be an object as any object in javascript can be created easily from a constructor. In fact, a Javascript's singleton is used as a namespace to provide a level of isolation from the global namespace and therefore served as a single point access to functions. A simple Singleton would look like this
With a bit of Javascript's famous closure, we can make the simpleSingleton to another level of complexity with private variables and functions.
My only question for this is that whether singleton in Javascript is really useful, I havent found any demonstrative sample yet. Comment below if you have any and want to share
Sunday, July 17, 2011
Thursday, July 14, 2011
Timezone convertion in Python
I have been googling this issue for several times and each time it manages to get out of my mind within a few days. So I finally decided to just blog it, for share and for later references.
Sunday, June 19, 2011
GROUP BY and ORDER By with MySQL
A few weeks ago, I have some good (and at the same time bitter) experience with MySQL that I would like to share.
Requirement: given the auction and bid tables, select user's latest bid for each auction that she joined
Lets look into two auctions of my sample user whose id is 25
As the target is to get the latest bid for each auction, below is my desired result
And here is the result of my first attempt:
Oops, something is wrong here (of course, otherwise why on earth am I writing this). The second row should have been the bid made in second 47 not 44. The reason for this error is that GROUP BY is performed before ORDER BY, in other words the ORDER BY doesnt have any effect here.
After googling for a while, I am pretty sure that there is no way to tell MySQL order first, group later. You will have to do the grouping and sorting manually. Below is my query:
That's a long and quite complicated query, however the result set is correct
A few days later a colleague looked at my code, saying that it was bad (definitely) and offered some help with this
However, that... is wrong. Although the processing time is reduced dramatically, this solution provides a wrong result set. You might be deceived if only concentrate on the datetime column. The id column is actually wrong, due to aggregation effect. But the query was successfully trigger a debate among us and finally illuminate a right solution
SHAME ON ME! Realizing that how ugly and ineffective my query is, is really painful. Anyway that is a valuable experience with group by and order by, who knows if later on it turns useful.
Monday, March 7, 2011
jQuery's ready function and the "this" keyword
A few days ago, I got some hard time when my belief about the 'simple' this keyword of jQuery busted.
I had this simple html code
And thank for #stackoverflow, I got my answer. After all, the $.ready() function is ALWAYS run on the document element, no matter what selector are you using. To find an evidence for this, I tried:
I had this simple html code
<div class="parent"> <div class="child"></div> <div class="child"></div> <div class="child"></div> </div>I simply want to count the number of children inside the parent div. And instead of writing the counting code inside $(document).ready() as I have always been writing, I decided to narrow down the scope to the parent div only (because I had no reason to wait for the whole doc to be ready) and that was where the story begun.
$("#parent").ready() { $(this).children().length; // return 1 $("#parent").children().length; // return 3 }There is something wrong here, within $("#parent").ready(), $(this) and $("#parent") selectors are supposed to refer to the same object, the parent div. But the result turned out to be something different.
And thank for #stackoverflow, I got my answer. After all, the $.ready() function is ALWAYS run on the document element, no matter what selector are you using. To find an evidence for this, I tried:
$("#parent").ready() { $(this).children().each(function(){ console.log($(this).html()); // the whole html }); $("#parent").children().each(function(){ console.log($(this).html()); // the children divs' content }); }Apart from increasing code's clarity and understandability, you should stick with $(document).ready() because that is where everything get executed at the end of the day.
Monday, December 27, 2010
Internship Presentation - RMIT 2010C
I had my internship presentation last week, perhaps the last presentation I ever had as an undergraduate student. There isn't anything make the presentation special, no new technology, no innovative idea; simply some lines to sum up what I was/am doing during this 4-month internship in Taipei.
Sunday, November 21, 2010
PHP? Fine with me
Have ever you heard "Ewww, you use PHP? I thought you were cool!"?
Despite having just been using PHP for 2 weeks for a company project, I have heard some of those from my colleagues who are using Python and Ruby for theirs. PHP has always been considered as creepy by many web developers, especially those who work on big systems with back-end and front-end components. I agree with those to certain points:
- PHP code is usually a mess of spaghetti where server code wraps SQL query and projects whatever to HTML
- PHP is by far not an expressive language, as not much as LISP family.
- PHP development is never actually designed but hacked but a group of people want to fill in their needs without any concerns about the language big picture.
That answers why you are very likely to run into
- Interface semantic story - the assumption about how the interface will be used which cant be enforced by the compiler. The semantic interface includes consideration such as "Method A must be called before Method B" (lack of design),
- Messy PHP code (the lack of expressive power)
- And a function with a perfect name but doesn't work while another with a 'more awkward' name does (it was HACKED by a group NOT a team)
Yet, despite of that you can find PHP here and there. PHP can make a good demonstration for worse-is-better principal, the messy code enables programmers develop sites with a few pages and interacts with the same number of db tables fast and easy, think about the pain you have to suffer if using an enterprise-standard solution such as J2EE. PHP is not well-known for its scalability but it does not mean it would take you 5 minutes to load a page and except client's target is the second Facebook, entrepreneur and start-up in general find PHP as an acceptable solution to have their projects. PHP also goes really with Apache (#mod_php) and thank to that, you can run PHP on almost every 10$/month hosting server.
Last but not least the core value of PHP has never been a complete server side language but a TEMPLATING SYSTEM, enabling developer to create dynamic content and at the same time keeping the code designer-readable. Many big guys are using PHP such as MailChimp, Yahoo and probably the most well-known: Facebook. Those manages to live with PHP while keeping their system scalable all because PHP is used as what it's meant to be: a templating language supported by a proper back-end. And with MVC framework such as Yii or Zen, where PHP core drawbacks such as class loading mechanism is resolved, PHP is going to live for a couple of years more.Friday, November 12, 2010
Internship timing - up and down
People have prejudice, some even love it. And my prejudice about internship time is that "In the whole year, between October and February is the worst time to look for an internship position". That thought came around a year ago when I learned the fact that at the end of the year, companies tend to finish off projects and wrap up the paperwork rather than optimize the management structure or looking for new positions. I once thought that it would be better if I could have my internship start at March.
Seems like I was wrong, again. As there is always something going wild in a software project, this period of time is like a sprint and much more interesting than a marathon. Along the way to the ultimate goal, a release, bends and ravines are just appearing from no where and those involving in that race do not even have time to take a break, let alone arguing with customers. This is the precious time where decisions are made, and different from the elaboration phase, these decisions are highly practical and many times go against what I was taught at school (badass practice)
Market at the end of the year is pretty lively with dozens of customer yelling "Gimme the web site, we need it for the Christmas campaign" (Christmas CDs) or "Look at the site, it is not biopeutic enough" and "we are considering changing the layout for the coming Christmas and new year" at the same time (customer's business is somewhere between biology and therapeutic). So these days the office runs at its full capacity, at least for the boss and the little intern who happens to live at the office. Crap, the red fat bastard is against him!!!
But only during such high time an intern can be put in charge for a new project (either because others got caught in ongoing projects or they get bored of such type of project already). And that experience is precious. You learn, in the hard and only way, to become a full-stack developer. It takes a lot of further practice, knowledge, and experience to become a growth hacker, but this indeeds a vey good start already.
Well, every coin has two sides, doesn't it? A dozen of bad things occur when year end and internship come together
- People are incredibly busy and asking a question is such a pain, regardless the importance and intelligence level of it.
- Researching new technologies and services are happily waiting 'till next Spring (and at Vietnam where cool stuff get blocked)
- Having the heart breaking feeling as if your app is crashed 10 minutes before assignment deadline for 2 months
- People cannot keep being calm all the time and the innocent intern has the tendency to become the source of trouble/argument even through he has little to do with the problem (or even didn't do a thing)
Oh man, Christmas "the celebration of the birth of commercialism..." people are sure trying to get the most out of it.
Subscribe to:
Posts (Atom)