SoFunction
Updated on 2025-04-13

10 Mini Tips for Beginners in JavaScript Page 2/2


7. Use objects as parameters to organize and improve functions
In modern web development, one of the most common uses of objects is to treat them as parameters of functions. It is always difficult to remember this rule for function parameters; however, using an object is very beneficial because we don't have to worry about the rule for parameter. Moreover, it is more organized and allows users to better understand what we are going to do. This method allows you to organize and improve functions using objects as parameters. For example:
Copy the codeThe code is as follows:

function insertData(name,lastName,phone,address){
code here;
}

The refactoring code looks like this:
Copy the codeThe code is as follows:

function insertData(parameters){
var name = ;
var lastName = ;
var phone = ;
var address = ;
}

It is also very useful when you want to use the default value. For example:
Copy the codeThe code is as follows:

function insertData(parameters){
var name = ;
var lastName = ;
var phone = ;
var address = ;
var status = || 'single' //If status is not defined as a property
//in the object the variable status take single as value
}

Now, it is very simple to use this function; we can send data in two ways:
Copy the codeThe code is as follows:

//Example 1
insertData({name:'Mike', lastName:'Rogers', phone:'555-555-5555',address:'the address', status:'married'});

//Example 2
var myData = { name:'Mike',
lastName:'Rogers',
phone:'555-555-5555',
address:'the address',
status:'married'
};
insertData(myData);

8. Functions are data
Functions are data like strings or numbers. We can pass them as function parameters, which can create surprising and "magnificent" web applications. This method is very useful, and almost all mainstream frameworks use this method. For example:
Copy the codeThe code is as follows:

function byId(element, event, f){
(element).['on'+event] = f; //f is the function that we pass as parameter
}
byId('myBtn','click',function(){alert('Hello World')});
Another example of functions as data:
//Example 1
function msg(m){
Alert(m);
}
//Example 2
var msg = function(m){ alert(m);}

These functions are almost identical. The only difference is how they are used. For example: the first function, you can use it before you declare it; but the second function can only be used after it is declared:
Copy the codeThe code is as follows:

//Example 1
msg('Hello world'); //This will work
function msg(m){
alert(m);
}
//Example 2
msg('Hello world'); //Does not work because JavaScript cannot find the function msg because is used before is been declared.
var msg = function(m){ alert(m)}

9. Extend local objects
Although some JavaScript leaders do not recommend this technology, it has been used by some frameworks. It allows you to create helper methods for JavaScript API.
Copy the codeThe code is as follows:

//We create the method prototype for our arrays
//It only sums numeric elements
= function(){
var len = ;
total = 0;
for(var i=0;i<len ;i++){
if(typeof this[i]!= 'number') continue;
total += this[i];
}
return total;
}
var myArray = [1,2,3,'hola'];
();


= function(){
return ('',this);
}

10,Boolean
Pay attention to the difference between them, as this will save you time debugging your scripts.
Copy the codeThe code is as follows:

'' == '0' // false
0 == '' // true
0 == '0' // true
false == 'false' // false
false == '0' // true
false == undefined // false
false == null // false
null == undefined // true
true == 1 // true
'' == null // false
false == '' // true

If you've read these scripts elsewhere, these tips can help you get it right. These tips are not even as the tip of the iceberg as all JavaScript features, but this is the beginning! Please don't be polite, leave your comments, questions, extra tips or concerns, but remember, this is an article for beginners! ! I hope to receive letters from some developers! Enjoy!
Previous page12Read the full text