Category Archives: Language

Javascript is an object-oriented language (1) – Encapsulation

Javascript is a powerful tool. No web developer can live without it. Without Javascript, the webs will be much poorer, as most of user-experience enhancement don’t exist.

Powerful tools are often misused. Javascript is in the same fate. Easy to write, easy to mess. And it soon earn the dispute as one of the most messy language. But investigating further, Javascript is one of the most mis-understood language.

Javascript is an object-oriented language.

That maybe a suprise to some people, and so do I. You see, javascript is often written in one big files, with the statement lined up from start to end… Global variables, C-like syntax,… it just seem to be a typical procedural language.

In fact, Javascript is heavily object-based. Everything in javascript is viewed as an object. Function is an object. Array is an object. But surely, “just because a language has objects doesn’t make it OO.”

You may say: but every property in a Javascript object is public? Aren’t we told that a language can only be called object-oriented when it has “encapsulation“, “inheritance” & “polymorphism“?

For the first question, I can confirm: yes. All property in a Javascript object is public. But Javascript is still an object-oriented-able language. Following I will show you how:

1. Encapsulation

Encapsulation can be achieved by a special feature of Javascript: the closure. Simply put, that’s the ability which allow the inner function has access to local variables in the outer function. A simple piece of code should says more than I could:

var Merlin = function() {
var say = function() { alert(advice); }

// Local variable that ends up within closure
var advice = 'One must complete what he started';
return {say: say};
}

var apprentice = new Merlin();
apprentice.say(); // alert 'One must complete what he started'
alert("Let's look inside of Merlin: " + advice); // alert nothing, since advice is not a global object

To be fully understand the concept of closure, one should study about how Javascript define scope for its object. In the above example, you can easily see that, the function say() can still access the local variable “advice”, even after function Merlin() has completed. Other languages (Java, C, for example) often requires the local variable disposed as soon as the function is complete. This is a strange mind-set, but as you see, it do the “hiding” job just well.

(to be continued)

P/s: Thanks to Mr. An to give his time explaining this to me, which most of the books never make a comment.

Wiki markups – Make simple things simple

With best wishes for sagisou – The weather is not good, hope you are fine and healthy.

wiki,markup,html

If you haven’t edited The Great Wikipedia anytime,  I recommend giving it a try. Since what I discuss here is related closely to them.

This is the story of Wiki markup language.

For a long time studying at university, I always wonder how people can build such complex architecture like .NET Platform, Java, since it took an ordinary student months of learning just to touch the surface. When I began going to work, that fact is reinforced day by day: I almost certain that I don’t fully understand 90% of the libraries used in my projects. “Just believe the creators and use them – you are only big if you stand on giants’ shoulder”. But a question pop out: if mankind continues building such high-level technology pyramid, how long it will take for them to forget the ground?

Enough chit-chatting. When I first come across Wiki-markup, it’s just like another tool out there. I don’t put the question why it is needed, although we already have the HTML, CSS, javascript… I believe such powerful tools can satisfy most the ambitious needs, (with a little dirty hack sometime :p). I never question anything until I have trouble with it, yes, the Wiki Markups.

Most of the syntax is not hard, but some part(especially table) is very very confused! Why they ever make a language like that??

The Wiki Markup Origin

According to Wikipedia, Wiki Markups is the kind of languages which born to ease the normal writers: who don’t need to be a good HTML coders. Well, you may say that HTML is not hard, but the fact is that it’s magic to non-technical guys. In short, a new tool is built to do a simple job, which can be done with current tools instead.

But it’s not the only reason.

The second reason: in Wikipedia (and any other kind of Wiki) – the focus is knowledge, the content, how are they organized, not how they are showed for the eyes. A simple syntax will solve that problem easily, without making too much trash of bad HTML code.

The third reason: the contributors can be anyone, who ranges from a clerk, a free student,… even a blind person, whoever have access to the Internet. If Wikis give them the powerful HTML/CSS/Javascript, not many people can use it. Even if some can use it, many line of HTML code will have severe holes about accessibility. HTML standard contains many strict rules so that the website can be read by a screen-reader for handicapted people, which most websites doesn’t care (If you don’t believe, take any website and go for the w3c HTML validator).

For all those reasons, I think Wiki Markup is a good idea. A good tool helps people do things better than before, and it’s doing a good job.

But it still has much to improve, or I will continuously complain about the marking-up tables.