Javascript,设计函数来引用自身
给出:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尽管如此,内联函数通常不受欢迎。
Although, inline functions are generally frowned upon.
好吧,我会回答不测试它,并使用prototypejs,但我相信如果你不想使用JS框架,你可以自己练习这些功能。
首先,您需要使用 document.createElement 创建 div,而不是注入 HTML,如下所示:
然后您必须使用 document.appendChild 将新的 div(变量 d)放入页面中。
现在是“困难”的部分:监听 mousedown 事件并采取相应的行动。您可以通过使用 Event.observe (prototypejs) 和 bind (prototypejs) 将对象绑定到侦听器来执行此操作,以便您可以使用其变量(this.variable1、variable2 等)。
这将给出类似于这样的代码:
一些注意事项:在没有 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:
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:
Some notes: bind and observe can be implemented somewhat easily without a JS framework.
This code is untested.
如果将函数定义为对象的属性,则
this
指向该对象:var myObject = { foo: true, bar: function(){ if(this.foo){ doStuff() ; }};
如果
this
在返回对象的构造函数中使用,则this
会绑定到返回的新对象。您在发布的代码中使用this
的方式,this
指向全局范围,这是 Javascript 的不好的部分之一。因此,我的处理方法如下:
我将 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, thenthis
is bound to the new object that is returned. The way you're usingthis
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:
I changed the ganttChart function to be capitalized because by convention, constructor functions that return an object are capitalized.