DEV Community

Mariusz Bialobrzeski
Mariusz Bialobrzeski

Posted on

JavaScript Context Thoughts

I am in a process of learning a new programming language. It is no surprise that the language is JavaScript. It seems like today every developer needs to know it. I used to work with .Net most of the time back in the days when writing code served as a mean to pay for the things I needed and the things I did not need. Today it is just a memory of the past which I learned to live with. The feature of C# I remember enjoying the most was it's static nature. Nicely defined variables with strong type that propelled intellisense when connecting pieces together. The satisfaction I had when those pieces clicked rewarded the extra time spend defining them. "Some people favor being explicit" remains my most beloved StackOverflow quote buried somewhere under those snappy comments below the popular answers. Yes there was an option to write dynamic code with var but that idea never really appealed to me. Now, with JavaScript, things are different. I have to rely more heavily on debugging due to silly mistakes such as variables not being defined. Overall this feels more like a script than a language to me. Take the function context as an example. You can set it to some other object to your liking by appending .call(), .apply() or .bind() to a function invocation. Then the function's context object can be accessed using 'this.'. Why would such a technique be of any benefit? How do I know what 'this' is referring to by looking at the function's definition? I would have to track down all the places where the function is called to have a feeing 'this.name' refers to a city name and not my cat's name for example. Why not just pass the the object to a function and name it properly instead? This seems like a hack added to the language in order to bridge some of the shortcomings. Of course I might be wrong about the purpose of such context alteration of a function. Anyway, it is OK to be wrong. That's inevitable part of a learning process.

Top comments (0)