Google Maps API call with JavaScript Example
Google Maps API call with JavaScript Example
javascript tutorials webGoogle Maps API call with JavaScript Example
Think Twice ... Code Once
1
2
3
4
| // Function declarationfunction function_name() {};// Function expressionvar function_name = function() {}; |
1
2
| function_name();//function call[WORKS]function function_name(){}; |
1
2
3
4
5
6
| var calc = { add : function(a,b){return a+b}, sub : function(a,b){return a-b} }console.log(calc.add(1,2)); //3console.log(calc.sub(80,2)); //78 |
add and sub functions are methods of calc object.
1
2
3
4
| function add(a){ return function(b){return a+b;}}console.log(add(1)(2)); // Output is 3 |
new keyword before a function and call it, it becomes a constructor that creates instances. Below is an example where constructors are used to create instances of Fruit and values are added to each Fruit‘s properties.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
| function Fruit(){ var name, family; this.getName = function(){return name;}; this.setName = function(value){name=value}; this.getFamily = function(){return family;}; this.setFamily = function(value){family=value}; }var apple = new Fruit();apple.setName("Apple");apple.setFamily("Apple family");var orange = new Fruit();orange.setName ("Orange");orange.setFamily ("Orange family");console.log(orange.getName()); // "Orange"console.log(apple.getName()); // "Apple"console.log(orange.getFamily()); // "Orange family" |