Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.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 无法访问类';在特定情况下,类的函数中的s变量_Javascript_Jquery_Javascript Events - Fatal编程技术网

Javascript 无法访问类';在特定情况下,类的函数中的s变量

Javascript 无法访问类';在特定情况下,类的函数中的s变量,javascript,jquery,javascript-events,Javascript,Jquery,Javascript Events,我不知道如何最好地解释这个问题,因为有很多事情要做,所以我继续并创建了一个我正在做的示例。代码如下: var cl = new Class(); cl.handleAction("triggerScream"); cl.handleAction('triggerDisplay'); function Class() { //function which will print static string this.scream = function(){ document.w

我不知道如何最好地解释这个问题,因为有很多事情要做,所以我继续并创建了一个我正在做的示例。代码如下:

var cl = new Class();
cl.handleAction("triggerScream");
cl.handleAction('triggerDisplay');



function Class()
{
  //function which will print static string
  this.scream = function(){
    document.write("AAAAAAA!!!<br/>");
  }


  //function which will print the class variable
  this.display = function(){
    document.write( this.color );
  };


  //sample class variable
  this.color = 'red';


  //map of actions
  this.actions = {
    'triggerDisplay' : this.display,
    'triggerScream' : this.scream
  };


  //generic function that handles all actions
  this.handleAction = function(action){
    try{
      this.actions[action]();
    }catch(err)
    {
      document.write("Error doing "+action);
    }
  };
}
var cl=newclass();
cl.手工过滤(“Triggersryan”);
cl.手动过滤(“触发显示”);
函数类()
{
//用于打印静态字符串的函数
this.cream=函数(){
文件。写(“AAAAAAA!!!
”; } //函数,该函数将打印类变量 this.display=函数(){ 文件。书写(此颜色); }; //样本类变量 this.color='red'; //行动图 此项操作={ “triggerDisplay”:this.display, “触发尖叫”:这是一声尖叫 }; //处理所有操作的通用函数 this.handleAction=函数(操作){ 试一试{ 这个.动作[动作](); }捕捉(错误) { 文件。写入(“错误执行”+操作); } }; }
这里是jsbin链接:

在总结中,有一个
handleAction()
函数,它处理各种事件并调用其他函数来完成事件。为此,我有行动事件和功能的地图来唤起。类的函数
display()
访问类变量,但由于某种原因
此函数在该函数中未定义。有没有关于为什么以及如何修复它的想法,这样我就可以访问变量,最好是保留代码体系结构


提前感谢。

调用函数时的作用域与类对象的作用域不同。这意味着“这”指的是其他东西:

function Class()
{
  //function which will print static string
  this.scream = function(){
    document.write("AAAAAAA!!!<br/>");
  }


  //function which will print the class variable
  this.display = function(){
    document.write( this.color );
  };

  //sample class variable
  this.color = 'red';



 //generic function that handles all actions
     this.handleAction = function(action){
    try{
      //you are calling the function in another scope
      this.actions[action]();
    }catch(err)
    {
      document.write("Error doing "+action);
    }
  };
}

调用函数时的作用域与类对象的作用域不同。这意味着“这”指的是其他东西:

function Class()
{
  //function which will print static string
  this.scream = function(){
    document.write("AAAAAAA!!!<br/>");
  }


  //function which will print the class variable
  this.display = function(){
    document.write( this.color );
  };

  //sample class variable
  this.color = 'red';



 //generic function that handles all actions
     this.handleAction = function(action){
    try{
      //you are calling the function in another scope
      this.actions[action]();
    }catch(err)
    {
      document.write("Error doing "+action);
    }
  };
}

这正是我所猜测的问题的原因。由于函数的大量重新分配,很难跟踪作用域。我将尝试这种解决方案。谢谢这正是我所猜测的问题的原因。由于函数的大量重新分配,很难跟踪作用域。我将尝试这种解决方案。谢谢
function Class()
{
  //we store the context
  var scope=this;
  //function which will print static string
  this.scream = function(){
    document.write("AAAAAAA!!!<br/>");
  }


  //function which will print the class variable
  this.display = function(){
    document.write(this.color);
  };


  //sample class variable
  this.color = 'red';


  //map of actions
  this.actions = {
    'triggerDisplay' : this.display,
    'triggerScream' : this.scream
  };


  //generic function that handles all actions
  this.handleAction = function(action){
    try{
      //we call our function in the Class context
      this.actions[action].call(scope);
    }catch(err)
    {
      document.write("Error doing "+action);
    }
 };
}
var cl = new Class();
cl.handleAction("triggerScream");
cl.handleAction("triggerDisplay");