Navigate to the console of the browser near you and type this simple function Person shown below and hit enter. Next, type Person followed by dot (.) see the methods on Person function. Pretty simple.
Thus the function is an object and has the methods as other objects in JavaScript.
We can call functions on function. Let’s invoke call function and see the output.
Invoking call method on function Person.
Prototype object:
Objects are keyed collection/associative array that has a hidden link to the prototype object. Try the following code in any browser console and understand it.
Object and prototype object has a linking.
We can access the prototype object using Object.prototype.
We can get an original object back from the prototype object using constructor (Object.prototype.constructor) property.
Pictorial representation of Object and it’s prototype object.
Oh, wait! We were talking about Function.
Like Object, Function is linked to Function.prototype.
Let me show you using Person function, it’s prototype object and their linking in the browser console.
Pictorial representation of Person function and it’s prototype object.
Design first: You understand requirement first via analysis, create use cases for it. Thus, the focus is on the requirement before writing the code.
Good enough code for now: You write sufficient code against use cases that fulfills the user requirements. You don’t write extra lines of unnecessary code.
Avoid rework: Due to design first approach, most of the doubts are cleared before writing code hence rework is avoided.
Developer confidence: Continous delivery of requirement in small cycles gives developer a confidence.
Regression catching.
Safe Refactoring: Once the code is covered by multiple tests, a developer can safely improve code structure with confidence and can focus on simple and elegant design.
A shared understanding of the business: Tests are the live documentation that is the communication channel between you and your future version or other developers that talks about business rules.
Clean code reminder: IMO, TDD always reminds you to make your code better than you found it. Of course, after covering your code with test, make it better.
Transpiled version of first code snippet. The days variable is initialised as an array and copies element from argument object.
Under the hood, the rest parameters are actually derived from arguments object of called function, `print`. Please notice carefully variable days on line# 5.
It’s value is set to an array whose length is equal to number of arguments passed.
Later, elements from arguments are copied to variable days (element by element from line# 8 to 10).
Another Example
Second snippet
In the second code snippet, please note there is a parameter multiplier apart from rest parameter ...numbers. Hence, copying will not begin from first element instead it started from second element till last one.
See transpiled version for more details.
Transpiled version
Transpiled version of second snippet. Please note argument objects first element is skipped.
I see many JavaScript developers have started replacing var with const just because some linter forces them to do so.
const in JavaScript is not only blocked scope but also const variable cannot be reassigned with new content.
It creates readonly reference. It means you cannot reassign value to the const variable. It does not guarantees immutability of the content that it holds.
const person = { name: 'John Doe', age: 33};
person = { name: 'John'}
// Above line will throw Uncaught TypeError:
// Assignment to constant variable.
// However, you can change the content of object
// that is referenced by person variable.
person.age = 40;
So, If you really want to mark your object as immutable then use some other API’s like `Object.freeze`.
There are specific use cases about const variable. Make sure you are intentionally marking some variable as const and not because some linter has that rule.