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();
}
}
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();
}
}
(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"
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();
Try:
function new1(){
var a = 5;
return a;
}
function new2(){
var c=6;
return c;
}
function new3(){
if(new1()<new2()){
// dosomething();
alert("done");
}
}
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!
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!
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();
}
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();
}
}
function new1(){
var a = 5;
new2(a);
}
function new2(a){
var c=6;
new3(a, c)
}
function new3(a, c){
if(a<c){
dosomething();
}
}