【问题标题】:Method autovivification方法自动激活
【发布时间】:2012-05-13 22:37:38
【问题描述】:

我正在考虑以下功能:

$('.aclass').MyPluginInit()
$('.aclass').SomeMethodOfMine()

但是我们如何从第一行到第二行呢?在理想的世界中,我能够捕获第 2 行中生成的异常(当我尝试调用一个不存在的方法时),循环遍历 $('.aclass') 代表的对象集,并查看每个对象包含所述方法的属性(例如,$this)并通过调用.MyPluginInit() 放置在那里。然后我会调用该方法。

问题是我无法捕获异常并找到返回引发异常的对象的方法。 window.onerror 的处理程序会告诉我产生异常的 url 和行号,但我无法将其绑定到对象。

有什么想法可以让我复活死者(或者在这种情况下从未出生)?

  • ekkis

附言我确实读过Autovivification and Javascript,但我要问的有点不同。

【问题讨论】:

  • 很难理解你的意思。可以编辑一下或者画个图吗?

标签: javascript jquery jquery-plugins


【解决方案1】:
// define your initializer
function MyPluginInit () {
  var e;
  for (e in this) {

    // give this object the method
    this[e].SomeMethodOfMine = function () {
      console.log("Wee!");
    }
  }
}

// call init using the array-ish thing returned by jQuery as `this`
MyPluginInit.call($(".aclass"));

// to call the method, you need to refer to an *element* in the array
// this is because we only gave the method to the elements, not to the array itself
$(".aclass")[0].SomeMethodOfMine();

我想不出一个很好的方法来做到这一点,但是这段代码是功能性的,不需要任何奇怪的全局异常处理。或者,您是否考虑过修改数组元素的原型?然后你只需要在你的方法中包含一些逻辑来确定在元素没有被“初始化”的情况下如何行动。

通常我会建议将SomeMethodOfMine 添加到jQuery 返回的对象的原型中,但结果是Object,所以这可能不是一个好主意。

【讨论】:

  • 感谢您的回复。是的,不完全在那里......必须取消引用并不漂亮,不,将它添加到 Object 原型不是一个好主意。不过我确实解决了。将在此处单独发布,以便我可以将其标记为答案
【解决方案2】:

这就是我想出的:在包含使用它的插件之前,将以下函数放入您包含的某个库中:

function PluginMaker() {
    var plugin = url2fn($('script:last').attr('src'));
    $.fn[plugin] = function (opts) {
        opts = $.extend(true, {}, $[plugin], opts);
        for (fn in opts.fx) this[fn] = fxmk(fn);    // auto-vivification of methods
        this.each(function () { if (!this['$' + plugin]) this['$' + plugin] = opts; });
        opts.init(opts);  // plugin initialisation
        this.init(opts);  // per-object initialisation
        return this;
    };
    function fxmk(nm) {
        return function () {
            var args = arguments;
            this.each(function () {
                this['$' + plugin].fx[nm].apply(this, args);
            });
            return this;
        };
    }
    return plugin;
}

然后像这样定义你的插件:

// -- myplugin.js ---------------------------------------------------------------

(function ($) {
    $[PluginMaker()] = {
        // whatever state data you want to keep for your plugin
        fx: {
            MyMethod1: function () { /* within these methods */ },
            MyMethod2: function (msg) { /* this refers to the HTML element */ },
            // whatever other methods you want to define
            init: function (opts) {
                // used for per-element initialisation
            }
        },
        init: function(opts) {
            // used for plugin initialisation (one time)
        }
    };
});    

那么,包含了你可以做的插件:

$('.class').MyPlugin({ /* whatever options */ });
$('.class').MyMethod1();

甚至:

$('#someId').MyMethod2();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-02
    • 2020-11-24
    • 2015-08-26
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    相关资源
    最近更新 更多