我如何在JavaScript中的函数之间传递变量 [英] How do i pass variables between functions in javascript

查看:90
本文介绍了我如何在JavaScript中的函数之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有两个最简单形式的函数。我正在与jquery合作。
将var str从第一个函数传递到第二个函数的最佳方式是什么?

 函数a(){
var str =first;
};

函数b(){
var new = str +second;
};


解决方案

您需要在它们之间传递它,或者它从你的例子看来,只需在更高的范围内声明它:

  var str; 
函数a(){
str =first;
}
函数b(){
var something = str +second; // new保留,使用另一个变量名称
}


Here are 2 functions in the simplest form. I'm working with jquery. What is the best way to pass var str from the first function to the second one.

function a() {
    var str = "first";
};

function b() {
    var new = str + " second";
};

解决方案

You need to either pass it between them, or it seems from your example, just declare it in a higher scope:

var str;
function a(){
  str="first";
}
function b(){
  var something = str +" second"; //new is reserved, use another variable name
}

这篇关于我如何在JavaScript中的函数之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆