javascript 如何在不声明 JS 全局变量的情况下将值从一个函数传递到另一个函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18755105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I pass a value from one function to another function without declaring a global variable in JS??
提问by user2767633
I just want to pass the value from my new1 and new new2 function to new3 function
我只想将 new1 和 new new2 函数的值传递给 new3 函数
function new1(){
var a = 5;
}
function new2(){
var c=6;
}
function new3(){
if(a<c){
dosomething();
}
}
回答by Paul
(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
演示
回答by Aditya Singh
Try this out:- http://jsfiddle.net/adiioo7/fBKub/
试试这个:- http://jsfiddle.net/adiioo7/fBKub/
JS:-
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();
回答by Unknown
Try:
尝试:
function new1(){
var a = 5;
return a;
}
function new2(){
var c=6;
return c;
}
function new3(){
if(new1()<new2()){
// dosomething();
alert("done");
}
}
回答by Nicole
This looks like you want either Object oriented programmingor namespacing...
OOP:
面向对象:
In this method, you create a constructorthat creates an object to hold your state (variables that are specific to that object) andmethods 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.
在这种方法中,它类似于全局变量,因为每个变量只存在一个。但是,由于命名空间(它只是一个简单的 JS 对象),您可以避免命名冲突。
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.
试试看。
回答by Vinay Pratap Singh
You can pass values from one function to other in java script simply like this
您可以像这样在java脚本中将值从一个函数传递到另一个函数
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.
在您的情况下,必须至少将 a 或 c 中的值之一声明为被调用函数的全局值,并且您可以传递一个值。
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();
}
回答by Sudhir Bastakoti
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();
}
}
回答by Ryan Schlueter
function new1(){
var a = 5;
new2(a);
}
function new2(a){
var c=6;
new3(a, c)
}
function new3(a, c){
if(a<c){
dosomething();
}
}
}