Every language has some features which are known to or are used by less number of people but that does not mean they are not of any use. They can come handy in certain situations. Below is a list of some such features of the most widely used language in web JAVASCRIPT. This list is not exhaustive by any means, it is compiled from various javascript resources and the nice and helpful developers always ready to help on various forums.
1) Want to know how many parameters are expected by a function, just get the value of length property of function.
It works because every function in javascript is the actually a function object. This length property is local to every function.
function add_nums(num1, num2, num3 ){
return num1 + num2 + num3;
}
add_nums.length // 3 is the number of parameters expected.
2) Know how many parameters are actually received by the function, use length property of arguments object.
function add_many_nums(a, b, c){
return arguments.length;
}
add_many_nums(2,1,122); //returns 3
You don't even need to define any parameters for a function. You can just use the function's arguments array-like object.
However, it is not recommended as it makes code less readable and hard to debug.
function sum() {
var retval = 0;
for (var i = 0, len = arguments.length; i < len; ++i) {
retval += arguments[i];
}
return retval;
}
sum(1, 2, 3) // returns 6
It is particularly useful if you want to perform some array-like operations on the arguments. Although it behaves like an array but keep in mind that it is still an object and you can not use any in-built array functions like join(), pop(), push(), etc.
However, you can get a real array like this :
var argArray = Array.prototype.slice.call(arguments);
You can get a real array from any object as long as it is array-like ie. Have numeric keys.
3) Recursion in anonymous functions.
Arguments variable has callee property which holds the reference to the currently executing function.
You can easily implement recursion using this property.
var result = function() {
if(condition){
arguments.callee();
}
}
4) If you have to use for() loop going forward, It is good to cache the length property.
var arr = new Array(2048);
for(var i==, j=arr.length; i<j; i++)
{
console.log(arr[i]);
}
5) Use parseInt() with caution.
Always pass a base to parseInt(). If you pass a string to parseInt() without informing it of the proper base, it will assume the base to be 8 and will return unexpected results.
// Incorrect
parseInt('010'); // return 8 instead of 10.
// Incorrect
parseInt('010', 10); // returns 10
6) Check if a key exists in an object using In operator.
In operator can be used to check whether a key exists in an object or not.
var x = 1, y = 3;
var obj = {0:0, 1:0, 2:0};
x in obj; //true
y in obj; //false
1 in obj; //true
y in {3:0, 4:0, 5:0}; //true
7) Object properties can also be accessed with [] instead of .(dot)
This is really useful when accessing properties whose name is not a legal identifier.
obj['class'] = demo; // class is a reserved identifier, obj.class is illegal.
obj['space separated names'] = demo; // Dot operator cannot be used here to access the property.
8) A function can return itself.
function display(arg){
console.log(arg);
return argument.callee();
}
display('One')('Two')('Three'); // Prints One, Two, Three
OR
(function(temp) {
console.log(temp);
return arguments.callee();
})('One', 'Two', 'Three'); // Prints One, Two, Three
10) Javascript does not have block scopes, use closures or self executing functions instead.
// Closure.
var x = 1;
{
var x = 2;
}
console.log(x); // outputs 2
// Self-Executing function
(function() {
var x= 2;
console.log(x);
})();
Happy Programming!
0 Comment(s)