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

Saturday, November 15, 2014

TIL: Plowing through the basics of JS actually helps

Today, I read through some pages of "JavaScript, the definitive Guide". I knew most of the stuff. And once again, like in almost every language, I read trough the seemingly boring bitwise operators. You know, stuff like |, &, ~, ^ and bit shift operators >>, << and >>>.

So I asked myself, why should I use them? Being and not-too-young developer and not having used them in any real-world-application yet, this shows me: Either those bit operators are a little bit outdated or I am a little bit ignorant, that means that I could miss out simpler solutions than I usually provide.

So I decided that I have to learn some bit operation magic.

So beside bit shift operations as an alternative to multiplying or dividing by powers of 2, I learned, that | 0 converts nearly everything to an integer.

Longer description can be found here:
http://stackoverflow.com/questions/654057/where-would-i-use-a-bitwise-operator-in-javascript

So, if you have some handy trickt with bitwise operators in JavaScript, let me know.

The other thing I heave learned: labels in Javascript. You can label stuff. Most likely, you will label loops, so that you can break free from them with with the break label; statement.

I have to say, that Mozilla discourages you from using labels.

Thursday, November 13, 2014

Today I learned: There is a use for void

Today, I learned two use cases for the Operator void
For the sake of Repetition: void is an operator which evaluates the given expression and returns undefined.

So, I asked myself, what could be modern day use cases for this operator?

I think there is no reason to be paranoid about undefined hijacking (i.e. setting the property undefined to something else than the value undefined.) Since ES 5, this is not allowed. And modern browser won't throw an Error in your face, when you do something like undefined = 5; but silently ignore your crappy code.

The other use, using it in the href attribute of a tags, is heavily discouraged today. In my questions on Google Plus and reddit, where I asked what sense people made out of the void operator, giving and old example of setting it into the href attribute, there were comments, begging not to do this.

So, finally, here the solution: first of all, you could use bookmarklets. What is a bookmarklet? Type javascript:3+7; in your browser's URL bar. Make sure, you use Firefox or IE (have it tested only in FF) to see the result. Congrats! You have executed your first bookmarklet. You can save this ingenious script to your bookmarks. What did it do? It evaluated 3+4 and changed your current page to that value. That's why you saw 7 in your browser. Webkit browsers don't do that, by the way. Never.

So void to the rescue: just type javascript:void(3+4) and nothing will be redirected. So you can implement a word counter bookmarklet.


Another use are minifiers.
Just like Yury Trabanko replied to me on Google+:



Minifiers use void operator. For example
function() {
    if(isA()) {
        return doB();
    }
    doC();
}


will become
function(){
    return isA()?doB():void doC();
}

Happy coding to you all!

Wednesday, November 12, 2014

Today I learned: void in JavaScript

Reading stuff about the JavaScript Module Pattern, about self-invoking anonymous functions (Tweet by Paul Irish , by the way, this tweet made me sign up to Twitter once again), I stumbled about undefined.

I knew that undefined was different. I knew that it was not a keyword. So what is it?
It's an variable. And a value.

It's a global variable, a property of the the global object, in the browser window.undefined.

I stumbled upon the the Mozilla documentation about undefined. As an alternative to
if (typeof lorenz === 'undefined')
they presented
if (lorenz === void 0)

I wondered. What's this void operator?
As the documentation succinctly explains:

The void operator evaluates the given expression and then returns undefined.
That's all. And then I remembered where I saw this weird little friend last time. It was about 1999 and it was about here:

<a href="javascript:void(0)" onclick="mouseOver()"><img src="earth_rotating.gif" /></a>

We've come a long way, baby. I'm glad we don't have to code this way any longer.

Anyway, now I think about a proper use case for this operator.