Three JavaScript Quirks That Java/C Developers Should Know

#Just another JS article
Congratulations, You have just added another article to the interwebz that shows how mis-understood JavaScript is. Not because you showcase any knowledge at all ofcourse. Your examples are wrong (did you even bother checking them?) and I think you didn’t even take the time to look up anything at all about the language.

##A bad start:

Your first example is wrong. It does not output ‘undefined, 0 5 5’. It does however output ’ undefined 0 0’. This because thx to hoisting the proper equivalent is this:

function myFunction() { 
  var i = undefined; //the compiler is smart enough to know we don't need to define i twice
  console.log(i);  //undefined
  i = 0; //simple assignment
  console.log(i); // 0
  if (false) { //the following if block is never executed
    i = 5;
    console.log(i);
  } 
  console.log(i); //0 since id did not change
}

##Prototype, functional, potatoe, potato, whatever
You state that JavaScript is a functional language, kind of. Not really, it looks like one. So lets call it one. Or whatever… Why force it to be anything else than what is really is? Why even try. It’s a prototype based language, and that is really powerful!

You also state a few lines later that function is a primitive. But it’s also an object. These two statements do not go well together. A quick fact checking on mdn would give us the following primitives:

  • string
  • number
  • Boolean
  • null
  • undefined

The mdn article also talks about primitive Wrappers. Objects that wrap around a primitive. This explains the behavior:

typeof new Number(5); //Object
typeof 5; //number

##Strange use of language
Normally, in computer science, we use words like ‘is a’. You don’t, you say

Function, the type of functions, is built upon Object

Not sure what you mean here, simply put. A Function is an Object. This because it is not a primitive. You then go on to ‘prove’ this by doing a typeof function.prototype . Not sure what your point here is. You have proven that the prototype is an object. Wow.

You also state that functions (can) have properties. Not sure if this is such a special thing, it’s an object…

And to explain closures, the following sentence does that really well: (taken from crockfords website)

An inner function always has access to the vars and parameters
of its outer function, even after the outer function has returned.

##Examples should explain
You end the article with some examples, not sure what they should prove. Your Person function is a factory-like function that returns an object that has access to a passed in variable ‘thanks to’ scope, closure, whatever. This doesn’t make the variable private, It just makes the variable a memory-leak. The variable also has nothing to do with the object. The object consists simply of 2 functions that have access to a non-local variable. Your point? Personally, if you want to show how to create an object with private vars, use privileged functions:

function Person(name) {
  var privateName = name; //don't touch me
  
  //define privileged functions:
  this.getName = function() {
    return privateName;
  }

  this.setName = function(name) {
    privateName = name;
  } 
}

var myPerson = new Person('foo'); //using new is so much cooler
myPerson.getName(); //foo
myPerson.privateName; //undefined

For people who would like to have a good read on scope and stuff: crockford

EDIT: I just can’t resist. You talk about JS being the first ‘mainstream’ language to implement closures. this shows that you or, have no idea what closures are, or with mainstream, you mean: ‘that I know’. The fist language implementing closures was somewhere around 1970 (if I remember correct). I wasn’t even born then…

Kind regards,
A sad reader

3 Likes