Javascript 无法在异步函数下访问此属性 lookforcustomers(值){ 如果(值=='on'){ 变量名称='subash'; this.intervalid=setInterval(异步函数getnearplaces(nameref){ console.log('running',this.name) //console.log('loc',outerthis.state.lastnownlocation.latitude) //var driverpoint=new Parse.GeoPoint({纬度:this.ref.state.lastnownlocation.latitude,经度:this.ref.state.lastnownlocation.longitude}); //var GameScore=Parse.Object.extend(“预订”); //const query=new Parse.query(PlaceObject); //query.equalTo(“已雇用”,假); //query.near(“pick”,driverpoint); //查询.限额(1); //const placesObjects=wait query.find()。然后((对象)=>{ //console.log(对象[0]) //},(错误)=>{ // //console.log('查找客户时出错') // }); }, 10000); } }

Javascript 无法在异步函数下访问此属性 lookforcustomers(值){ 如果(值=='on'){ 变量名称='subash'; this.intervalid=setInterval(异步函数getnearplaces(nameref){ console.log('running',this.name) //console.log('loc',outerthis.state.lastnownlocation.latitude) //var driverpoint=new Parse.GeoPoint({纬度:this.ref.state.lastnownlocation.latitude,经度:this.ref.state.lastnownlocation.longitude}); //var GameScore=Parse.Object.extend(“预订”); //const query=new Parse.query(PlaceObject); //query.equalTo(“已雇用”,假); //query.near(“pick”,driverpoint); //查询.限额(1); //const placesObjects=wait query.find()。然后((对象)=>{ //console.log(对象[0]) //},(错误)=>{ // //console.log('查找客户时出错') // }); }, 10000); } },javascript,Javascript,我只需要从getNearPlacesFunction()访问名称值。当我尝试记录名称值时,它不会打印。是否有其他程序访问名称值或上述代码中是否存在任何缺陷?如果您的意思是要访问分配给此行中定义的变量name的值: var name = 'subash'; 您只需省略this的使用,因为内部函数getnearplaces可以访问它所包含的上下文中定义的变量 因此: console.log('running', name) 应打印字符串: running subash 为了更好地理解闭包在Ja

我只需要从
getNearPlacesFunction()
访问名称值。当我尝试记录名称值时,它不会打印。是否有其他程序访问名称值或上述代码中是否存在任何缺陷?

如果您的意思是要访问分配给此行中定义的变量
name
的值:

var name = 'subash';
您只需省略
this
的使用,因为内部函数
getnearplaces
可以访问它所包含的上下文中定义的变量

因此:

console.log('running', name)
应打印字符串:

running subash
为了更好地理解闭包在JavaScript中的工作方式,您可能需要阅读以下内容:


希望这有帮助

尝试将异步函数更改为箭头函数:

lookforcustomers(value){
  if (value == 'on') {
    var name = 'subash';

    this.intervalid = setInterval(async (nameref) => {
      console.log('running', this.name)

      //  console.log('loc',outerthis.state.lastknownlocation.latitude)

      // var driverpoint = new Parse.GeoPoint({latitude:this.ref.state.lastknownlocation.latitude, longitude:this.ref.state.lastknownlocation.longitude});
      //  var GameScore = Parse.Object.extend("Booking");
      // const query = new Parse.Query(PlaceObject);
      // query.equalTo("hired",false);
      // query.near("pick", driverpoint);
      // query.limit(1);
      //  const placesObjects = await query.find().then((object) => {
      //    console.log(object[0])
      //  }, (error) => {
      //
      //  console.log('error while looking for the customers')
      //  });

    }, 10000);
  }
}

我希望这有帮助。

您是否尝试调用
this.intervalid()
?还有一个问题,您正在将此调用到
getnearplaces()
函数中,因此
关键字代表了这一点。如果要这样调用,应该将关键字绑定到函数
getnearplaces.bind(this)
。对不起,我英语不好