Javascript jQuery从另一个函数收集函数内部的数据

Javascript jQuery从另一个函数收集函数内部的数据,javascript,jquery,function,Javascript,Jquery,Function,函数运行时是否可以从另一个函数收集数据 //常用函数 function collectingData(){ var name = 'tom'; return name } //我的目标在这里 $('.myDiv').click(function(){ // here I want data from another function // collectingData() if( name == 'tom' ){ //here I have to

函数运行时是否可以从另一个函数收集数据

//常用函数

function collectingData(){
   var name = 'tom';
   return name
}
//我的目标在这里

$('.myDiv').click(function(){

   // here I want data from another function
   // collectingData() 

  if( name == 'tom'  ){
     //here I have to finish based of data
  }

})
试试这个

   $('.myDiv').click(function(){

   // here I want data from another function
   // collectingData() 
   let name = collectingData();   


  if( name == 'tom'  ){
     //here I have to finish based of data
  }

})
试试这个

   $('.myDiv').click(function(){

   // here I want data from another function
   // collectingData() 
   let name = collectingData();   


  if( name == 'tom'  ){
     //here I have to finish based of data
  }

})

我是新来的,所以我不能将此作为注释发布,但是有什么原因不能在单击处理程序中运行
collectingData()
,如
let name=collectingData()
?这将是在两者之间传递数据的最简单方法。

我是新来的,所以我不能将此作为注释发布,但有没有原因不能在单击处理程序中运行
collectingData()
,如
let name=collectingData()
?这将是在两者之间传递数据的最简单方法。

你可以!只要收集数据的函数不是异步函数

如果它是一个异步函数,那么根据该函数的返回数据,我们将使用适当的方法来处理该返回数据


处理异步函数有两种常见模式:基于回调和基于承诺!只要收集数据的函数不是异步函数

如果它是一个异步函数,那么根据该函数的返回数据,我们将使用适当的方法来处理该返回数据


处理异步函数有两种常见模式:基于回调和基于承诺;所以,定义一个变量,做任何你想做的事情

 $('.myDiv').click(function(){
   // here I want data from another function
   // collectingData() 
   var name = collectingData();
  if( name == 'tom'  ){
     //here I have to finish based of data
  }

})

您已从CollectionData()返回名称;所以,定义一个变量,做任何你想做的事情

 $('.myDiv').click(function(){
   // here I want data from another function
   // collectingData() 
   var name = collectingData();
  if( name == 'tom'  ){
     //here I have to finish based of data
  }

})

为什么不通过删除前面的var`,使name
成为一个全局变量呢

function collectingData(){
   name = 'tom';
 //  return name
}
名称将自动显示在整个脚本中

$('.myDiv').click(function(){

   // here I want data from another function
   // collectingData() 

  if( name == 'tom'  ){
     //here I have to finish based of data
  }

})

为什么不通过删除前面的
var`,使name
成为一个全局变量呢

function collectingData(){
   name = 'tom';
 //  return name
}
名称将自动显示在整个脚本中

$('.myDiv').click(function(){

   // here I want data from another function
   // collectingData() 

  if( name == 'tom'  ){
     //here I have to finish based of data
  }

})
这样称呼:-

function collectingData(){
  name = 'tom';
  return name
}

 $('.myDiv').click(function(){
 // here I want data from another function
 // 
 var name ='tom'
 if( name == collectingData() ){
 //here I have to finish based of data
 }

})
这样称呼:-

function collectingData(){
  name = 'tom';
  return name
}

 $('.myDiv').click(function(){
 // here I want data from another function
 // 
 var name ='tom'
 if( name == collectingData() ){
 //here I have to finish based of data
 }

})