1

I just want to pass the value from my new1 and new new2 function to new3 function

function new1(){
  var a = 5;
}

function new2(){
  var c=6;
}

function new3(){
  if(a<c){
    dosomething();
  }
}
firien
  • 1,548
  • 16
  • 23
user2767633
  • 57
  • 1
  • 1
  • 7
  • Return the variable from your functions, and call those functions within your 3rd function? – Chris Rockwell Sep 12 '13 at 03:59
  • see [closures](http://stackoverflow.com/questions/111102/how-do-javascript-closures-work) – firien Sep 12 '13 at 04:01
  • please don't post any more duplicate questions, or the system will ban you from asking questions at all. – Andrew Barber Sep 12 '13 at 05:08
  • This should be marked as a duplicate of https://stackoverflow.com/questions/407048/accessing-variables-from-other-functions-without-using-global-variables and/or https://stackoverflow.com/questions/111102/how-do-javascript-closures-work. The current duplicate has been deleted. – Ilmari Karonen Jul 16 '19 at 16:19

7 Answers7

5
(function(){

  var a, c; // Not global, but available to all three functions

  function new1(){
    a = 5;
  }

  function new2(){
    c = 6;
  }

  function new3(){
    if(a < c)
      doSomething();        
  }

  new1();
  new2();
  new3();

})();

function doSomething(){
  console.log("Doing something");
}

console.log(typeof a); // "undefined"

Demo

Paul
  • 139,544
  • 27
  • 275
  • 264
  • nice answer. but maybe wrap it up so functions can be called outside the closure: http://jsfiddle.net/MkRKD/ – firien Sep 12 '13 at 04:20
  • if any function containing any object like 'function new1(obj1){ var a = 5; return a; } function new2(obj2){ var c=6; return c; }' – user2767633 Sep 12 '13 at 04:39
3

Try this out:- http://jsfiddle.net/adiioo7/fBKub/

JS:-

function new1() {
    this.a = 5;
}

function new2() {
    this.c = 6;
}


function new3() {
    var objnew1= new new1();
    var objnew2= new new2();
    if (objnew1.a < objnew2.c) {
        alert("a less than b");
    }
}

new3();
Aditya Singh
  • 9,512
  • 5
  • 32
  • 55
  • i think having to call `new1` and `new2` from within `new3` sort of defeats the purpose. – firien Sep 12 '13 at 04:33
  • if any function containing any object like 'function new1(obj1){ var a = 5; return a; } function new2(obj2){ var c=6; return c; }' – user2767633 Sep 12 '13 at 04:40
1

Try:

function new1(){

    var a = 5;
    return a;
}

function new2(){
var c=6;
    return c;
}

function new3(){
     if(new1()<new2()){
    // dosomething();
         alert("done");
       }
}

DEMO FIDDLE

Kiran
  • 20,167
  • 11
  • 67
  • 99
1

This looks like you want either Object oriented programming or namespacing...

OOP:

In this method, you create a constructor that creates an object to hold your state (variables that are specific to that object) and methods to operate on those variables.

You can have many versions of that object, each with their own state.

function dosomething() {
  alert("Did something!");
}

var MyNewObject = function() {
  // Accessible to functions because the functions are closures.
  var a=0; 
  var c=0;

  this.new1 = function(){
    a=5;
  }

  this.new2 = function(){
    c=6;
  }

  this.new3 = function(){
    if (a < c){
      dosomething();
    }
  }
}

var objThatDoesNews = new MyNewObject();
objThatDoesNews.new3(); // Didn't do anything
objThatDoesNews.new1(); // Set a to 5
objThatDoesNews.new2(); // Set c to 6
objThatDoesNews.new3(); // Did something!

Try it.

Namespacing:

In this method, it is similar to global variables in that only one of each variable exists. However, because of the namespace (which is just a simple JS object), you avoid naming conflicts.

function dosomething() {
  alert("Did something!");
}

var MyNamespace = {
  a: 0,
  c: 0,

  new1: function(){
    this.a=5;
  },

  new2: function(){
    this.c=6;
  },

  new3: function(){
    if(this.a < this.c){
      dosomething();
    }
  }
}

MyNamespace.new3(); // Didn't do anything
MyNamespace.new1(); // Set a to 5
MyNamespace.new2(); // Set c to 6
MyNamespace.new3(); // Did something!

Try it.

Nicole
  • 32,841
  • 11
  • 75
  • 101
0

You can pass values from one function to other in java script simply like this

function func1(){
var c=6;
var a =5;
func2(c,a);
 }

function func2(obj1, obj2){
 //some operation here using obj1 and obj2
}

but you want to pass values from different methods, it will have two different calls and the called function will not hold values till the next call.

In Your case at least one of the values in a or c will have to be declared global for called function, and you can pass one.

Alternative

One other alternative can be that you can have function with return values, and you can directly call function inside another functions.

function func1(){
 var c = 5;
  return c;
 }

function func2(){
 //use func1 here
 func1();
}
0

you could just return values, like:

    function new1(){
      var a = 5;
      return a;
    }

    function new2(){
        var c=6;
        return c;
    }


    function new3(){
       //get values
       var x = new1(), y = new2();
       if(x < y){
           dosomething();
      }
}
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0
function new1(){

    var a = 5;
new2(a);
}

function new2(a){
var c=6;
new3(a, c)
}


function new3(a, c){
     if(a<c){
     dosomething();
       }

}

Ryan Schlueter
  • 2,223
  • 2
  • 13
  • 19