如何将类方法分配给默认属性(即从静态范围引用非静态方法)

时间:2022-09-01 16:53:25

I have a plugin that has extendable transitions (used when new panels of data are shown). It comes with default transition methods of:

我有一个具有可扩展转换的插件(在显示新的数据面板时使用)。它带有默认的转换方法:

  • "none" - just replace the data
  • “无” - 只需替换数据

  • "slide" - slide out the existing panel and slide in the new one
  • “滑动” - 滑出现有面板并滑入新面板

  • "fade" - fade out the existing panel then fade in the new one
  • “淡出” - 淡出现有面板然后淡入新面板

The various transition methods all exist as properties of a transitions property on my default options object:

各种转换方法都作为我的默认选项对象上的transitions属性的属性存在:

e.g.

module MyPlugins
{
    export class MyPlugin
    {
        static defaultOptions =
        {
            // Transition methods - can be extended to add new transitions
            transitions:
            {
                none: function (transition): JQueryPromise<any>
                {
                    return transition.$panel.toggle(transition.inward).promise();
                },
                slide: function (transition: TransitionEventParams): JQueryPromise<any>
                {
                    return this._slide(transition);
                },
                fade: function (transition: TransitionEventParams): JQueryPromise<any>
                {
                    return this._fade(transition);
                }
            }
        }

        // Greatly simplified example class method
        private _fade(transition: TransitionEventParams): JQueryPromise<any>
        {
            var $panel = transition.$panel;
            if (transition.inward)
            {
                return $panel.fadeIn(transition.duration).promise();
            }
            else
            {
                return $panel.fadeOut(transition.duration).promise();
            }
        }
    }
}

At runtime the transition methods are called by name, using the plugin as the current this:

在运行时,转换方法按名称调用,使用插件作为当前的:

 promise = this.o.transitions[transition].call(this, transitionParams);

Now my question is: Is it possible to reference the class methods, from the static defaults, so that the anonymous function wrappers are not needed

现在我的问题是:是否可以从静态默认值引用类方法,以便不需要匿名函数包装器

e.g. I want to be able to have something like:

例如我想能够有这样的东西:

static defaultOptions =
{
    // Transition methods - can be extended to add new transitions
    transitions:
    {
        ...
        slide: MyPlugin._slide,
        fade: MyPlugin._fade
    }

But I can't figure out how to reference non-static class methods, except by name (e.g. object["methodname"].call(this, params)), from a static object.

但我无法弄清楚如何引用非静态类方法,除了名称(例如object [“methodname”] .call(this,params)),来自静态对象。

Any ideas on how to simplify my default options? Should I just make the helper methods static too, given that the plugin will be the value of this?

有关如何简化默认选项的任何想法?我是否应该让辅助方法也是静态的,因为插件将是这个值?

1 个解决方案

#1


You should be able to access the "instance" methods statically on the prototype, like

您应该能够在原型上静态访问“实例”方法,例如

transitions: {
    …
    slide: MyPlugin.prototype._slide,
    fade: MyPlugin.prototype._fade
}

#1


You should be able to access the "instance" methods statically on the prototype, like

您应该能够在原型上静态访问“实例”方法,例如

transitions: {
    …
    slide: MyPlugin.prototype._slide,
    fade: MyPlugin.prototype._fade
}