最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

passing variables across functions on javascript - Stack Overflow

programmeradmin0浏览0评论

I'm trying to pass variables across functions,
something like

function one() {
  var x = 1
}

function two() {
  var y = x + 1;
}

alert(y);

I'm trying to pass variables across functions,
something like

function one() {
  var x = 1
}

function two() {
  var y = x + 1;
}

alert(y);

Is there a way to do it?

edit:Thanks everyone for being so helpful, but maybe I should have been more specific with my question.

Share Improve this question edited Dec 20, 2018 at 4:26 pete asked Dec 19, 2018 at 22:45 petepete 1352 silver badges14 bronze badges 2
  • 1 Please show us how you intend to call the functions. Without calling them, no assignment is done and no value is ever created. – Bergi Commented Dec 19, 2018 at 22:54
  • Also please explain why you want/need to do this? Having function accept arguments and return values is a much better design. – Felix Kling Commented Dec 19, 2018 at 23:52
Add a ment  | 

5 Answers 5

Reset to default 3

You current way has x and y in the scope of the function, which means the other function doesnt know it exists. Also, its good practice to name functions according to what they do. 3 straightforward ways to do this.

  1. Global
  2. Params
  3. Inline

  1. Set two variables outside of the functions scope that any function can reach.

var x, y;

function assignOne() {
   x = 1;
}

function addOne() {
  y = x + 1;
}

assignOne();
addOne();
console.log(y);

  1. Pass in a parameter to to the function and return values.

function one() {
   return 1;
}

function addOneTo(x) {
  return x + 1;
}

const y = addOneTo(one());
console.log(y);

  1. Perform functions inline

var x = null;

function one() {
   x = 1;
}

function two() {
  return x + 1;
}

one();
const y = two();
console.log(y);

You will need to hoist the scope, by extracting the variable declaration to outside of the functions. That is to say, define x and y outside of the functions. Note that you can still update their values from within the functions. However, don't forget that you'll actually need to invoke both functions as well!

This can be seen in the following:

var x, y;

function one() {
   x = 1;
}

function two() {
  y = x + 1;
}

one();
two();
console.log(y);

If you really want to get variable declared in one method, return it

  function one(){
        var x = 1;
        return x;
    }
    
    function two() {
      var x = one();
      var y = x + 1;
      return y;
    }
alert(two());

Seems like you want to have shared state between both functions instead of passing arguments. So, an object oriented pattern seems to be appropriate.

class Thing {
  constructor() {
    this.x = 1;
  }
  one() {
    return this.x;
  }
  two() {
    return this.x + 1;
  }
}

const t = new Thing();
console.log(t.one());
console.log(t.two());

If you want to share variables between functions but don't want to declare them in global scope, you can use a closure like this:

(function() {
  var x, y;

  function one() {
    var x = 1
  }

  function two() {
    var y = x + 1;
  }
  one();
  two();
  alert(y);
})();
发布评论

评论列表(0)

  1. 暂无评论