Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/404.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在回调函数中调用函数会抛出角度错误_Javascript_Cordova - Fatal编程技术网

Javascript 在回调函数中调用函数会抛出角度错误

Javascript 在回调函数中调用函数会抛出角度错误,javascript,cordova,Javascript,Cordova,我使用具有两个回调函数的 window.plugins.screensize.get(this.screensize_success, this.screensize_error); 接下来,在成功回调中,我执行以下操作: screensize_success(result) { console.log(result); // <--- works fine console.log("##### height: "+result.height); // <--- w

我使用具有两个回调函数的

window.plugins.screensize.get(this.screensize_success, this.screensize_error);
接下来,在成功回调中,我执行以下操作:

screensize_success(result)
{
    console.log(result); // <--- works fine
    console.log("##### height: "+result.height); // <--- works fine
    this.get_highest(result); // <--- throws an error
}
get_highest(result)
{
  return Math.max(result.height,result.width);
}
screensize\u成功(结果)
{

console.log(result);//调用回调时,您将丢失
上下文

解决此问题的最简单方法是将回调绑定到
this

window.plugins.screensize.get(this.screensize_success.bind(this), this.screensize_error.bind(this));

调用回调时,您将丢失
上下文

解决此问题的最简单方法是将回调绑定到
this

window.plugins.screensize.get(this.screensize_success.bind(this), this.screensize_error.bind(this));

我个人更喜欢将函数设置为易于在程序中重用的变量,例如:

var get_highest = function (result)
{
  return Math.max(result.height,result.width);
}
如果没有这个,就叫它:

get_highest(result); 

我个人更喜欢将函数设置为易于在程序中重用的变量,例如:

var get_highest = function (result)
{
  return Math.max(result.height,result.width);
}
如果没有这个,就叫它:

get_highest(result);