www.webdeveloper.com
Results 1 to 5 of 5

Thread: [RESOLVED] Objective Javascript beginner

  1. #1
    Join Date
    Aug 2012
    Posts
    3

    resolved [RESOLVED] Objective Javascript beginner

    I'm new to Objective javascript even I had a good amount of experience in Javascript. How do I pass my parameters to the calc closure 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 the parameters to calc like (calc.multiply(10,20));

    Thanks in advance..

  2. #2
    Join Date
    May 2012
    Location
    St. Helens, UK
    Posts
    74
    Do you mean this: http://en.wikipedia.org/wiki/Objective-J (I'd never heard of it before just now) or do you just mean object oriented programming in JavaScript? (I'm assuming the latter.)

    As far as closures go, this is the best documentation I've found on the subject: https://developer.mozilla.org/en-US/...Guide/Closures

    Anyway. I'm not 100% sure what you're trying to do here, but I'm assuming you probably want the option of calling calc.multiply with no arguments and retrieving the product of a and b, or passing arguments to calc.multiply and changing a and b in the process. In which case you could do this:

    Code:
    var calc = (function(){
    var a;
    var b;
    return {
    setOperands: function(x,y) {
        a = x; b = y;
    },
    add: function(){
        return a+b;
    },
    subtract: function(){
        return a-b;
    },
    multiply: function(x,y){
        if (!((x === undefined) || (y === undefined))) {
            a = x; b = y;}
        return a * b;
    },
        divide: function(){
            if(b!=0)
            return a/b
            else{
                alert('division by zero');
                return false;
                }
            }
        }
    })();
    
    console.info(calc.multiply());      // NaN
    calc.setOperands(5,5);
    console.info(calc.multiply());      // 25
    console.info(calc.multiply(5,12));  // 60
    console.info(calc.multiply());      // 60
    Was that what you had in mind, sort of?

  3. #3
    Join Date
    Jul 2011
    Posts
    30
    Code:
    var calc = (function(){
    	var a;
    	var 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;
    			}
    		},
    		set operands(ops) {
                		a = Number(ops.split(",")[0]);
    			b = Number(ops.split(",")[1]);
            	}		
    	}
    })();
    calc.operands = "3,2"
    alert(calc.add())

  4. #4
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    here a simple way to do about what you want:

    Code:
    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'll have to mod each method to accept formal arguments...

  5. #5
    Join Date
    Aug 2012
    Posts
    3
    Thank you rnd_me. That is just the answer I wanted. Thank you.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles