Javascript 原型函数和 SVG setAttribute(onclick)

发布于 2024-10-14 16:21:52 字数 387 浏览 4 评论 0 原文

尝试使用 svg onClick 来调用原型函数。

通常要调用原型函数,我只会这样做。(functionName),但是当我将其放入 .setAttribute(onclick, "this.(functionName)") 时,它无法识别原型函数。有人有这方面的经验吗?

如果上面的内容不清楚,这是它的基本要点......

function myobject(svgShape) {
    this.svgshape.setAttribute(onclick, 'this.doSomething()');
}
myobject.prototype.doSomething = function() {
    alert("works");
}

Trying to use an svg onClick to call a prototype function.

Usually to call a prototype function I would just do this.(functionName) but when I put it into the .setAttribute(onclick, "this.(functionName)") it does not recognise the prototype function. Has anyone had any experience in this?

In case the above wasn't clear heres the basic jist of it...

function myobject(svgShape) {
    this.svgshape.setAttribute(onclick, 'this.doSomething()');
}
myobject.prototype.doSomething = function() {
    alert("works");
}

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

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

发布评论

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

评论(1

浅语花开 2024-10-21 16:21:52

三件事可能会有所帮助:

1)首先,我认为您在 myobject 函数顶部缺少这一行:

this.svgshape = svgshape;

我假设这只是发布问题的错误并已插入该行以下。

2) 通常,当您使用 Prototype(或任何现代库)时,您不会使用字符串进行回调,而是使用函数。此外,您通常使用库的包装器来分配处理程序 addEventListener / attachEvent (observe,在 Prototype 的情况下)而不是旧的 DOM0 属性。所以:

function myobject(svgShape) {
    this.svgshape = svgshape;
    $(this.svgshape).observe('click', this.doSomething); // STILL WRONG, see below
}
myobject.prototype.doSomething = function() {
    alert("works");
}

3) 但是 JavaScript 没有方法(它并不真正需要它们),它只有函数,所以上面的内容不能确保 this (调用的上下文)设置正确。对于 Prototype,您可以使用 bind 来设置上下文:(

function myobject(svgShape) {
    this.svgshape = svgshape;
    $(this.svgshape).observe('click', this.doSomething.bind(this));
}
myobject.prototype.doSomething = function() {
    alert("works");
}

或者你可以使用你自己的闭包来做到这一点。bind的优点是闭包处于一个非常受控的环境中,因此不会关闭你不关心的东西)

现在,我从未使用 Prototype 进行过任何 SVG 编程,因此如果 observe 由于某种原因不起作用,您可以尝试直接分配给 onclick 反射属性:

function myobject(svgShape) {
    this.svgshape = svgshape;
    this.svgshape.onclick = this.doSomething.bind(this);
}
myobject.prototype.doSomething = function() {
    alert("works");
}

我仍然在那里使用 bind ,以便 this 具有正确的值。

我贫血的小博客中的这些帖子提供了对上述内容的更多讨论:

Three things that may help:

1) First off, I think you're missing this line from the top of your myobject function:

this.svgshape = svgshape;

I'm assuming that was just an error posting the question and have inserted that below.

2) Normally when you're using Prototype (or any modern library), you don't use strings for callbacks, you use functions. Also, you normally assign handlers using the library's wrapper for addEventListener / attachEvent (observe, in Prototype's case) rather than the old DOM0 attribute thing. So:

function myobject(svgShape) {
    this.svgshape = svgshape;
    $(this.svgshape).observe('click', this.doSomething); // STILL WRONG, see below
}
myobject.prototype.doSomething = function() {
    alert("works");
}

3) But JavaScript doesn't have methods (it doesn't really need them), it just has functions, so the above won't ensure that this (the context of the call) is set correctly. With Prototype you'd use bind to set the context:

function myobject(svgShape) {
    this.svgshape = svgshape;
    $(this.svgshape).observe('click', this.doSomething.bind(this));
}
myobject.prototype.doSomething = function() {
    alert("works");
}

(Or you can use your own closure to do it. The advantage of bind is that the closure is in a very well-controlled environment and so doesn't close over things you don't want kept around.)

Now, I've never done any SVG programming with Prototype, so if observe doesn't work for some reason, you might try directly assigning to the onclick reflected property:

function myobject(svgShape) {
    this.svgshape = svgshape;
    this.svgshape.onclick = this.doSomething.bind(this);
}
myobject.prototype.doSomething = function() {
    alert("works");
}

I'm still using bind there so that this has the correct value.

These posts from my anemic little blog offer more discussion of the above:

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