Javascript,设计函数来引用自身

发布于 2024-10-05 21:20:58 字数 1098 浏览 4 评论 0原文

给出:

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!
    }

}

Given:

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>";
....
}

How would I make the function gInitBarDrag() defined inside the ganttChart class? I don't want an exterior standalone function as it needs to reference things inside the object.

So for example, the function would be able to reference variable1/2/3 which are defined in a specific instance of the ganttChart object (you can have multiple chart objects).

Hope that makes sense! EG:

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!
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

暮倦 2024-10-12 21:20:58
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);
}
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);
}
断念 2024-10-12 21:20:58
this.gContainer.innerHTML += "<div id=\"gBar" + i + "\" class=\"gBar\" onmousedown=\"function(){ alert('here'); }\">Hello</div>";

尽管如此,内联函数通常不受欢迎。

this.gContainer.innerHTML += "<div id=\"gBar" + i + "\" class=\"gBar\" onmousedown=\"function(){ alert('here'); }\">Hello</div>";

Although, inline functions are generally frowned upon.

北音执念 2024-10-12 21:20:58

好吧,我会回答不测试它,并使用prototypejs,但我相信如果你不想使用JS框架,你可以自己练习这些功能。

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

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

然后您必须使用 document.appendChild 将新的 div(变量 d)放入页面中。
现在是“困难”的部分:监听 mousedown 事件并采取相应的行动。您可以通过使用 Event.observe (prototypejs) 和 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 框架的情况下,绑定和观察可以很容易地实现。
该代码未经测试。

OK, I will answer without testing it, and using prototypejs, but I'm sure you can workout the functions for yourself if you don't want to use a JS framework.

First, you will want to create your div using document.createElement instead of injecting HTML, something like this:

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

Then you will have to use document.appendChild to put your new div (variable d) in your page.
Now the "difficult" part: listen for mousedown events and act accordingly. You can do this by using Event.observe (prototypejs) AND bind (prototypejs) to bind your object to the listener, so you can use its variables (this.variable1, variable2, etc).

This will give a code somewhat like this:

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));

} 

Some notes: bind and observe can be implemented somewhat easily without a JS framework.
This code is untested.

紫瑟鸿黎 2024-10-12 21:20:58

如果将函数定义为对象的属性,则 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 函数更改为大写,因为按照惯例,返回对象的构造函数是大写的。

If you define a function as a property of an object, then this points to the object:

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

If this is used in a constructor function that returns an object, then this is bound to the new object that is returned. The way you're using this in the code you posted, this points to the global scope, which is one of the bad parts of Javascript.

So, here's how I would approach this:

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>";

I changed the ganttChart function to be capitalized because by convention, constructor functions that return an object are capitalized.

梦断已成空 2024-10-12 21:20:58
var gInitBarDrag = function(){
  ///function body here
};

function ganttChart(gContainerID) {
....

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


}
var gInitBarDrag = function(){
  ///function body here
};

function ganttChart(gContainerID) {
....

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


}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文