【问题标题】:Javascript, design function to reference itselfJavascript,设计函数来引用自己
【发布时间】:2010-12-03 17:10:26
【问题描述】:

给定:

myChart = new ganttChart("chart1");

function ganttChart(gContainerID) {

    this.variable1 = "lol";
    this.variable2 = "hai dere";
    this.variable3 = "cometishian";

....
    this.gContainer = document.getElementById(gContainerID);
    this.gContainer.innerHTML += "<div id=\"gBar" + i + 
            "\" class=\"gBar\" onmousedown=\"gInitBarDrag(this)\">Hello</div>";
....
}

如何在 ganttChart 类中定义函数 gInitBarDrag()?我不想要一个外部独立函数,因为它需要引用对象内部的东西。

因此,例如,该函数将能够引用在 ganttChart 对象的特定实例中定义的变量 1/2/3(您可以有多个图表对象)。

希望这是有道理的!例如:

function ganttChart(gContainerID) {

    this.variable1 = "lol";
    this.variable2 = "hai dere";
    this.variable3 = "cometishian";

....
    this.gContainer.innerHTML += "<div id=\"gBar" + i + 
            "\" class=\"gBar\" onmousedown=\"gInitBarDrag(this)\">Hello</div>";
....

    gInitBarDrag = function(thisGanttObject)
    {
        alert(thisGanttObject.variable2);
        // This line wont work because it doesn't know what ganttChart to reference!
    }

}

【问题讨论】:

  • @Tom this 值指向什么?
  • @Tom 你所说的“granttChart 类”是什么意思? granttChart 是一个函数,反正 JavaScript 中没有类。
  • 我认为您可以尝试附加一个事件而不是在标记中声明它,并在 ganttChart 中引用该函数。现在的大多数 JS 框架都应该有一个 API 来将事件附加到 DOM 对象。我没有发布答案,因为我现在无法尝试编写代码。如果我回到家时还没有回答,我会尝试为您获取一些工作代码。
  • @Tom Aha,那么它是一个构造函数吗?
  • 我已经改写了,是的,innerHTML 部分是对象构造函数的一部分

标签: javascript function object


【解决方案1】:
function gnattChart(gContainerID) {
  var div = document.createElement('div');
  div.id = 'gBar';
  div.className = 'gBar';

  //If you want to reference properties from the gnattChart object...
  //Use `self` where you'd normally use `this` in the handler function
  var self = this;

  div.onmousedown = function () {
    //contents of gInitBarDrag here...
  };

  //If you want the container to be emptied...
  this.gContainer.innerHTML = '';

  this.gContainer.appendChild(div);
}

【讨论】:

  • +1 应该可以。 (虽然我更喜欢使用库附加事件处理程序)
  • @Šime Vidas - 在谈论 Javascript 时,很容易偏离正题,不是吗。只是试图让事情变得简单。我也更喜欢它,幸运的是,使用onevent DOMNode 属性是 100% 跨浏览器,并且至少比内联事件属性好一个数量级,即使不是完全理想。
【解决方案2】:
this.gContainer.innerHTML += "<div id=\"gBar" + i + "\" class=\"gBar\" onmousedown=\"function(){ alert('here'); }\">Hello</div>";

虽然,内联函数通常不受欢迎。

【讨论】:

  • 对不起,您错过了我的问题点,该函数需要能够访问主 ganttChart 对象中的变量
【解决方案3】:

好的,我会在不测试的情况下回答它,并使用prototypejs,但是如果您不想使用JS框架,我相信您可以自己锻炼这些功能。

首先,您需要使用 document.createElement 创建您的 div,而不是注入 HTML,如下所示:

var d = document.createElement("div");
// Set your div properly.

然后您将不得不使用 document.appendChild 将您的新 div(变量 d)放入您的页面。 现在是“困难”部分:监听 mousedown 事件并采取相应措施。您可以通过使用 Event.observe (prototypejs) AND bind (prototypejs) 将您的对象绑定到侦听器来做到这一点,这样您就可以使用它的变量(this.variable1、variable2 等)。

这将给出一个有点像这样的代码:

function ganttChart(gContainerID) { 

this.variable1 = "lol"; 
this.variable2 = "hai dere"; 
this.variable3 = "cometishian"; 

// Create Element part (createElement, appendChild, etc).
var d = document.createElement("div");
// continue from here.


this.gInitBarDrag = function() 
{ 
    alert(this.variable2); 
}; 

$(d).observe("mousedown", this.gInitBarDrag.bind(this));

} 

一些注意事项:绑定和观察可以在没有 JS 框架的情况下轻松实现。 此代码未经测试。

【讨论】:

    【解决方案4】:

    如果将函数定义为对象的属性,则this指向该对象:

    var myObject = { foo: true, bar: function(){ if(this.foo){ doStuff(); }};

    如果在返回对象的构造函数中使用this,则this 将绑定到返回的新对象。您在发布的代码中使用 this 的方式,this 指向全局范围,这是 Javascript 的坏处之一。

    所以,这就是我的处理方法:

    function GanttChart(gContainerID) {
        var chart = {
        variable1: "lol",
        variable2: "hai dere",
        variable3: "cometishian",
        containerId: gContainerID,
        gInitBarDrag: function(){
            alert(this.variable2);
            }
        }
        return chart;
    }
    myChart = new GanttChart("chart1");
    document.getElementById(myChart.containerId).innerHTML += "<div id=\"gBar" + i + "\" class=\"gBar\" onmousedown=\"myChart.gInitBarDrag();\">Hello</div>";
    

    我将 ganttChart 函数更改为大写,因为按照惯例,返回对象的构造函数是大写的。

    【讨论】:

      【解决方案5】:
      var gInitBarDrag = function(){
        ///function body here
      };
      
      function ganttChart(gContainerID) {
      ....
      
          this.gContainer.innerHTML += "<div id=\"gBar" + i + "\" class=\"gBar\" onmousedown=\"gInitBarDrag(this)\">Hello</div>";
      ....
      
      
      }
      

      【讨论】:

      • 该函数能否引用包含在 ganttChart 对象中的变量?
      • 不...它会寻找一个名为gInitBarDrag的全局函数,找不到,然后抛出异常
      • 复制粘贴错误。修好了
      猜你喜欢
      • 1970-01-01
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 2016-11-29
      • 2012-05-29
      相关资源
      最近更新 更多