x = 1;
var a = 5;
var b = 10;
var c = function(a, b, c) {
var x = 10;
document.write(x); //i
document.write(a); //ii
var f = function(a, b, c) {
b = a;
document.write(b); //iii
b = c;
var x = 5;
}
f(a,b,c);
document.write(b); //iv
}
c(8,9,10);
document.write(b);
document.write(x);
Answers:
c(8,9,10); ->
10 //i
8 //ii
8 //iii
9 //iv
document.write(b); -> 10
document.write(x); -> 1
| JavaScript Function | JavaScript Methods |
| Block of codes that performs specific tasks | Property of an object that contains function definition |
| Function can pass the data that is operaated and may returned the data | Method operates data that contained in a class |
| Function can be called by its name | A method consists of a code that can be called by the name of its object and its method name using dot notation or square bracket notation.. |
function aFunc(parameters) {
// Content
}
aFunc(); // function call
|
var student = {
fullname : function() {
// Content
}
};
student.fullname() // method call
|
In a constructor function this does not have a value. It is a substitute for the new object. The value of this will become the new object when a new object is created.
There are lexical scope where a closure is created and free variale is used in javascript.
var aObj = {
name : "fred",
major: "music",
smallestOfTwo: function(a,b){
if(a>b){
return b;
}
else if(a==b){
return a*a;
}
else{
return a;
}
}
}
function Employee(name, salary, position) {
this.name = name;
this.salary = salary;
this.position = position;
}
var emp1 = new Employee("Km Hira", 4000, "Software-Developer");
var emp2 = new Employee("Vishnu", 2000, "Junior-Developer");
var emp3 = new Employee("Anne", 6000, "Manager");
function product(x, y, ...more) {
var total = x * y;
if (more.length > 0) {
for (var i = 0; i < more.length; i++) {
total *= more[i];
}
}
return total;
}
Another way:
function productOfArguments() {
let i;
let productResult = 1;
for (i = 0; i < arguments.length; ++i) {
productResult *= arguments[i];
}
return productResult;
}
var maxOfThree = (a,b,c) =>{
let max = Math.max(a,b);
return Math.max(c,max);
}