10 secrets about JavaScript

Software Engineer | Full Stack Web developer | Javascript | React.js | Python | PHP | AWS | Docker
# 10 secrets about JavaScript Ah! JavaScript… In the early 2000s, we all saw what JS was used for: making animations! Some will remember, in particular, the “neon” texts and other nonsense that was done in the past. We shall see in this article 10 secrets in Javascript.
Fortunately, our perception of the language has evolved and we now use JS as a true imperative programming language. Today, I’m sharing 10 JavaScript secrets that make it the language it is.
1 – Node.js did not invent anything
On December 4, 1995 (~23 years old), JS was born at Netscape. A few days later, in December 1995, Netscape developed a server-side scripting language called Server-Side JavaScript (SSJS). In 1996, Microsoft launched an IIS module for JScript, its copy of JavaScript. It will be necessary to wait until 2009 for node.js to see the light of day, that is to say almost 15 years.
2 – The language was originally called LiveScript
When the language was first integrated into the Netscape Navigator browser (ouch… memories!), the code name was Mocha, then was renamed LiveScript in September 1995. Finally, the name JavaScript was adopted on December 3, 1995.
3 – JS has no abstraction
No abstract class, no interface. Difficult to comply with SOLID principles…
var test;
function log(what) {
console.log(what);
}
6 – There is more than one type of scope for variables
As seen in point 5, variables created with var exist before being physically declared in the code. This is because var creates the variable on the first pass of code analysis, which is called function-scoped. On the contrary, const and let produce traditional variables that exist at the block-scoped level.
```var var1; // function-scoped, exists everywhere in the current function
```let var1; // bloc-scoped, exists from here
7 – The keyword this is not the instance of the current object
In JS, this refers to the context of the caller, not the current object instance. For instance :
```var myObject = { test: function () { console.log(this); // this = setTimeout } }
setTimeout(myObject.test, 0); // Delegation // ^---- Context of caller
```var myObject = {
test: function () {
console.log(this); // this = myObject
}
}
setTimeout(function () { myObject. test(); }, 0); // Delegation
// ^---- Context of caller
Here, this is equal to the instance of the current object, i.e. myObject. To change this, we can do:
```var myThis = { isThis: true };
var myObject = { test: function () { console.log(this); // this = myThis } }
setTimeout(function () { myObject.test.call(myThis); // ^---- Context of caller }, 0);
## 8 – Even if it is impossible, we can have private-ish variables
Thanks to the pattern module specific to JS, it is possible to use variable scopes (scope) to emulate the concept of private members:
```var myObject = (function () {
// function-scoped, only exist at
// inside this function.
var privateVariable = 'Hello World';
function privateMember() {
console.log(privateVariable);
}
function publicMember() {
privateMember();
}
return {
publicMember: publicMember // Public exposure
};
})();
myObject. publicMember();
myObject.privateMember(); // boom
Being a prototype-based language, a subclass of object-oriented, it is possible for our objects to have several types by cloning the prototypes:
function A() {}
functionB() {}
B.prototype = Object.create(A.prototype);
console.log(new B() instanceof Object); // Displays true
console.log(new B() instanceof A); // Displays true
console.log(new B() instanceof B); // Displays true
10 – It is possible to hack JavaScript
The term may be a bit strong, but it is possible to modify the native code of JavaScript to replace it with ours. For instance :
function hackConsole() {
var delegate = console.log; // Keep the ref
console.log = function (what) {
delegate(new Date() + ' ' + what);
}
}
hackConsole();
console. log('Hello world'); // Displays the date + Hello world
You could, for example, disable all console.log in production by overriding the method with a function that does nothing 😉
Conclusion
You have learned today, I hope, 10 little secrets about JavaScript. To continue your learning, I invite you to consult the official JS documentation. Make it your bible 🙂
Also, if you’re feeling generous, you can buy me a beer to encourage me to produce free, ad-free content for you, a wonderful community of developers.
Please share with your friends and colleagues on social media!
In our next class, we shall be talking about loops in javascript
