Javascript
How is almost everything in Javascript an object?
Patching numbers
You can add your own methods to wrapper objects like Number or String.
Number.prototype.isOne = function() { return Number(this) === 1; }; (1.0).isOne(); // -> true (1).isOne(); // -> true (2.0).isOne(); // -> false (7).isOne(); // -> false
💡 Explanation:
Obviously, you can extend the Number object like any other object in JavaScript.
Template literals (Template strings)
If you have used graphql then you might be writing something like this
export const LAUNCH_TILE_DATA = gql` fragment LaunchTile on Launch { __typename id isBooked rocket { id name } mission { name missionPatch } }
`;
This is just a Template Literal
We all know the simple things like
`some string ${any_variable} ${fun_val()} or you can write in next line `
But You can Call a function !...
What Calling functions with backticks
Let's declare a function that logs all params into the console:
function f(...args) { return args; }
No doubt, you know you can call this function like this:
f(1, 2, 3); // -> [ 1, 2, 3 ]
This function basically returns the arguments array
But did you know you can call any function with backticks?
f`true is ${true}, false is ${false}, array is ${[1, 2, 3]}`; // -> [ [ 'true is ', ', false is ', ', array is ', '' ], // -> true, // -> false, // -> [ 1, 2, 3 ] ]
Or we can say that
f`${arg1}${arg2}`