Datt Dongare
I've expertise in web development using Ruby on Rails and ReactJS. I help teams to write better code and also mentor them for organizing the projects efficiently
function is composed of a sequence of statements called the function body. Values can be passed to a function, and the function will return a value.
The Function constructor creates a new Function object. In JavaScript every function is actually a Function object.
A function definition (also called a function declaration, or function statement) consists of the function keyword, followed by:
function square(number) {
return number * number;
}
functions can also be created by a function expression. Such a function can be anonymous;
var factorial = function fac(n) { return n < 2 ? 1 : n * fac(n - 1); };
console.log(factorial(3));
_______________________________________________________
function map(f, a) {
var result = [], // Create a new Array
i;
for (i = 0; i != a.length; i++)
result[i] = f(a[i]);
return result;
}
var multiply = function(x) { return x * x * x; }; // Expression function.
map(multiply, [0, 1, 2, 5, 10]);
a function defined in the global scope can access all variables defined in the global scope. A function defined inside another function can also access all variables defined in its parent function and any other variable to which the parent function has access.
// The following variables are defined in the global scope
var num1 = 20, num2 = 3, name = 'Chamahk';
// This function is defined in the global scope
function multiply() {
return num1 * num2;
}
multiply(); // Returns 60
// A nested function example
function getScore() {
var num1 = 2, num2 = 3;
function add() {
return name + ' scored ' + (num1 + num2);
}
return add();
}
getScore(); // Returns "Chamahk scored 5"
function addSquares(a, b) {
function square(x) {
return x * x;
}
return square(a) + square(b);
}
a = addSquares(2, 3); // returns 13
b = addSquares(3, 4); // returns 25
var createPet = function() {
var name;
return {
setName: function(newName) {
name = newName;
},
getName: function() {
return name;
}
}
}
var pet = createPet('Vivie');
pet.getName(); // Vivie
pet.setName('Oliver');
var getCode = (function() {
var secureCode = '0]Eal(eh&2';
// A code we do not want outsiders to be able to modify...
return function() {
return secureCode;
};
}());
getCode(); // Returns the secureCode
function myConcat(separator) {
var result = ''; // initialize list
var i;
// iterate through arguments
for (i = 1; i < arguments.length; i++) {
result += arguments[i] + separator;
}
return result;
}
// returns "red, orange, blue, "
myConcat(', ', 'red', 'orange', 'blue');
// returns "elephant; giraffe; lion; cheetah; "
myConcat('; ', 'elephant', 'giraffe', 'lion', 'cheetah');
1. Default params
- parameters of functions default to undefined
function multiply(a, b) {
b = typeof b !== 'undefined' ? b : 1;
return a * b;
}
multiply(5); // 5
// New syntax
function multiply(a, b = 1) {
return a * b;
}
multiply(5); // 5
2. Rest params
- syntax allows us to represent an indefinite number of arguments as an array
function multiply(multiplier, ...theArgs) {
return theArgs.map(function(x) { return multiplier * x});
}
var arr = multiply(2, 1, 2, 3);
console.log(arr); // [2, 4, 6]
// Function Constructor
var multiply = new Function('x', 'y', 'return x * y');
// function declaration
function multiply(x, y) {
return x * y;
} // there is no semicolon here
// function expression of an anonymous function assigned to multiply
var multiply = function(x, y) {
return x * y;
};
var multiply = function func_name(x, y) {
return x * y;
};
// Difference
var y = function x() {};
alert(x); // throws an error
function sayHello(firstName, secondName) {
console.log(`${this.sayHello()} ${firstName} ${secondName}`);
}
var context = {
sayHello() {
return 'Hello';
}
}
const firstName = 'Alex';
const secondName = 'Perry';
sayHello.call(context, firstName, secondName); //Hello Alex Perry
- same as invoking a function as you would do normally, however the difference is that you also get to specify the functions context
function sayHello(firstName, secondName) {
console.log(`${this.sayHello()} ${firstName} ${secondName}`);
}
var context = {
sayHello() {
return 'Hello';
}
}
const firstName = 'Alex';
const secondName = 'Perry';
sayHello.apply(context, [firstName, secondName]); //Hello Alex Perry
- exactly the same as call apart from the fact that you pass in the functions arguments as an array and not separately.
function sayHello(firstName, secondName, middleName) {
console.log(`${this.sayHello()} ${firstName} ${middleName} ${secondName}`);
}
var context = {
sayHello() {
return 'Hello';
}
}
const firstName = 'Alex';
const secondName = 'Perry';
const middleName = 'James';
const boundFunc = sayHello.bind(context, firstName, secondName);
boundFunc(middleName); //Hello Alex James Perry
var s,
NewsWidget = {
settings: {
numArticles: 5,
articleList: $("#article-list"),
moreButton: $("#more-button")
},
init: function() {
s = this.settings;
this.bindUIActions();
},
bindUIActions: function() {
s.moreButton.on("click", function() {
NewsWidget.getMoreArticles(s.numArticles);
});
},
getMoreArticles: function(numToGet) {
// $.ajax or something
// using numToGet as param
}
};
/** file contents of assets/global.js */
//= require common/library.js
//= require module/news-widget.js
//= require module/some-other-widget.js
(function() {
NewsWidget.init();
SomeOtherModule.init();
})();
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function
https://alexperry.io/personal/2016/04/03/How-to-use-apply-bind-and-call.html
https://css-tricks.com/how-do-you-structure-javascript-the-module-pattern-edition/
By Datt Dongare
Write javascript better way. Understand the functions and advanced javascript. Javascript modular pattern.
I've expertise in web development using Ruby on Rails and ReactJS. I help teams to write better code and also mentor them for organizing the projects efficiently