Pass parameter: Objective javascript

I am new to Objective javascript, even I had good experience in Javascript. How to pass my parameters to calc closing here?

var calc = (function() {
    var a = 5;
    var b = 0;
    return {
        add: function() {
            return a + b;
        },
        subtract: function() {
            return a - b;
        },
        multiply: function() {
            return a * b;
        },
        divide: function() {
            if (b != 0) return a / b
            else {
                alert('division by zero');
                return false;
            }
        }
    }
})();​

    console.log(calc.divide());

I want to pass parameters for calculation like (calc.multiply (10,20));

Thanks in advance.

+5
source share
7 answers

Here is an easy way!

var calc = function(a,b) {
    return {
        add: function() {
            return a + b;
        },
        subtract: function() {
            return a - b;
        },
        multiply: function() {
            return a * b;
        },
        divide: function() {
            if (b != 0) return a / b
            else {
                alert('division by zero');
                return false;
            }
        }
    }
};

//console.log(calc.divide());
//I want to pass the parameters to calc like (calc.multiply(10,20));

calc(10,20).multiply();

otherwise, you will have to modify each method to accept formal arguments ...

+2
source

According to your calling method calc.multiply(a, b): should be the following:

var calc = (function() {
    return {
        add: function(a, b) {
            return a + b;
        },
        subtract: function(a, b) {
            return a - b;
        },
        multiply: function(a, b) {
            return a * b;
        },
        divide: function(a, b) {
            if (b != 0) return a / b
            else {
                alert('division by zero');
                return false;
            }
        }
    }
})();

console.log(calc.divide(20, 2));
+2
source
var calc = function(a,b) {
    return {
        add: function() {
            return a + b;
        },
        subtract: function() {
            return a - b;
        },
        multiply: function() {
            return a * b;
        },
        divide: function() {
            if (b != 0) return a / b
            else {
                alert('division by zero');
                return false;
            }
        }
    }
};
+1

:

var calc = (function() {
    var a = 5;
    var b = 0;
    return {
        add: function() {
            return a + b;
        },
        subtract: function() {
            return a - b;
        },
        multiply: function() {
            return a * b;
        },
        divide: function(n, d) {
            if (n != 0) return n / d
            else {
                alert('division by zero');
                return false;
            }
        }
    }
})();

alert(calc.divide(3, 4));​
0

:

var calc = (function() {
    var a = 5;
    var b = 0;

    return {
        add: function(a, b) {
            return a + b;
        },
        subtract: function(a, b) {
            return a - b;
        },
        multiply: function(a, b) {
            return a * b;
        },
        divide: function(a, b) {
            if (b !== 0) return a / b;
            else {
                alert('division by zero');
                return false;
            }
        }
    };
})();

console.log(calc.multiply(10, 10));

a b divide, .

, .

0
var calc = (function() {
    return {
        add: function(a, b) {
            return a + b;
        },
        subtract: function(a, b) {
            return a - b;
        },
        multiply: function(a, b) {
            return a * b;
        },
        divide: function(a, b) {
            if (b != 0) return a / b
            else {
                alert('division by zero');
                return false;
            }
        }
    }
})();​

Although I would think about changing your warning to an error.

0
source

You can specify the parameters calcthat the object returns to calculate something with two numbers, or just create an object calcwith functions that calculate something. Your requirement calc.multiply(10,20)for the second solution:

var calc = {
    add: function(a, b) {
        return a + b;
    },
    subtract: function(a, b) {
        return a - b;
    },
    multiply: function(a, b) {
        return a * b;
    },
    divide: function(a, b) {
        if (b != 0) return a / b
        alert('division by zero');
        return false;
    }
};​

Otherwise, for use calc(10,20).multiply(), this will be the construction of the closure that you already have, but with parameters instead of two local constants:

function calc(a, b) {
    return {
        add: function() {
            return a + b;
        },
        subtract: function() {
            return a - b;
        },
        multiply: function() {
            return a * b;
        },
        divide: function() {
            if (b != 0) return a / b
            alert('division by zero');
            return false;
        }
    };
}
0
source

All Articles