简体   繁体   English

纯Javascript插件开发

[英]Pure Javascript plugin development

I need to develop a pure javascript plugin wich can be accessed like jquery typed plugins ( $('.pluginWrapper').pluginInit(); 我需要开发一个纯JavaScript插件,可以像jquery类型的插件( $('.pluginWrapper').pluginInit();一样进行访问$('.pluginWrapper').pluginInit();

However i need to to use pure javascript and i was thinking maybe about 2 supported formats: 但是我需要使用纯JavaScript,我在想也许支持2种格式:

document.getElementById('pluginWrapper').pluginInit();
pluginInit(document.getElementById('pluginWrapper'));

I know that you have to do an IIFE to wrap it and call it via object methods but i do not know how i can bind that to an element. 我知道您必须做一个IIFE来包装它并通过对象方法调用它,但是我不知道如何将它绑定到一个元素上。

I am mentioning that i am a begginer so please can someone please explain something about this. 我是说我是一个初学者,所以请有人可以解释一下。 Cheers! 干杯!

You would be safer developing a plugin interface that simply exposes plugins as functions which take an element as an argument. 您将更安全地开发一个插件接口,该接口仅将插件公开为以元素为参数的函数。

function MyPlugin(element) {
  // do stuff with element
  element.setAttribute('data-plugin-id', 'pluginName');
}

The other approach involves extending Element.prototype . 另一种方法涉及扩展Element.prototype Which could potentially be a dangerous action in production software. 这可能是生产软件中的危险动作

However, it is still possible. 但是,仍有可能。

Element.prototype.pluginInit = function() {
  // the element is accessible as `this`
  this.setAttribute('data-plugin-id', 'pluginName');
}

A function is easy for everyone to understand. 每个人都易于理解的功能。 Plugin writers don't have to understand any interfaces for creating and registering plugins, they just need to know that they should write functions that take elements as arguments. 插件编写者不必了解用于创建和注册插件的任何接口,他们只需要知道他们应该编写将元素作为参数的函数即可。

There's a great talk from Rich Hickey (the creator of Clojure) called Simplicity Matters in which he stresses that the worst thing you can do is add additional complexity when simple solutions will do. Rich Hickey(Clojure的创建者)有一个很棒的演讲,名为Simplicity Matters ,他强调说,您可以做的最坏的事情是,当简单的解决方案可用时,会增加额外的复杂性。

In this case, you don't need anything more complex than a function which takes an element as an argument. 在这种情况下,除了将元素作为参数的函数之外,您不需要任何其他复杂的事情。


If it is completely essential that you have control of the function, you could write a simple interface for registering and initiating plugins. 如果必须完全控制该功能,则可以编写一个简单的界面来注册和启动插件。

function Plugin(element) {
  if(element === null) {
    throw new TypeError("Element must not be null!");
  }

  // get all the plugin names from the store
  var pluginNames = Object.keys(Plugin.store);

  // make sure `this` is set to the element for each plugin
  var availablePlugins = pluginNames.reduce(function(plugins, name) {
    plugins[name] = Plugin.store[name].bind(element);
    return plugins;
  }, {});

  // return an object containing all plugins
  return availablePlugins;
}

// we'll store the plugins in this object
Plugin.store = {};

// we can register new plugins with this method
Plugin.register = function(name, pluginFn) {
  Plugin.store[name] = pluginFn;
};

Which you could use like this. 您可以这样使用。

Plugin.register('myPlugin', function() {
  this.setAttribute('data-plugin-id', 'myPlugin');
});

Plugin(document.getElementById('pluginWrapper')).myPlugin();

If you want the plugin function to take a selector, the same way as jQuery, then you can use document.querySelectorAll inside your definition for Plugin . 如果您希望插件函数采用选择器(与jQuery相同),则可以在您的Plugin定义内使用document.querySelectorAll

function Plugin(selector) {
  var element = document.querySelectorAll(selector);

  if(element === null) {
    throw new TypeError("Element must not be null!");
  }

  // get all the plugin names from the store
  var pluginNames = Object.keys(Plugin.store);

  // make sure `this` is set to the element for each plugin
  var availablePlugins = pluginNames.reduce(function(plugins, name) {
    plugins[name] = Plugin.store[name].bind(element);
    return plugins;
  }, {});

  // return an object containing all plugins
  return availablePlugins;
}

Then you would use it like this instead. 然后,您可以像这样使用它。

Plugin.register('myPlugin', function() {
  this.setAttribute('data-plugin-id', 'myPlugin');
});

Plugin('#pluginWrapper').myPlugin();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WordPress 自定义插件开发中的纯 JavaScript 基本 AJAX 调用 - Pure JavaScript Basic AJAX Call In WordPress Custom Plugin Development Javascript插件开发,扩展库 - Javascript Plugin Development , Extending Libraries 用于在jsp中进行javascript开发的Eclipse插件? - Eclipse plugin for javascript development in jsp? Javascript模板系统 - PURE,EJS,jquery插件? - Javascript template system - PURE, EJS, jquery plugin? WordPress插件开发 - 如何使用JQuery / JavaScript? - WordPress Plugin Development - How to use JQuery / JavaScript? 纯JavaScript本地开发Python后端如何集成IBM Watson Assistant服务 - How to integrate IBM Watson Assistant services in Pure JavaScript local development Python backend Phonegap Cordova 2.2插件开发,无法在Java和JavaScript之间进行通信 - Phonegap Cordova 2.2 plugin development, cannot communicate between Java and JavaScript Eclipse插件开发-JavaDoc像JavaScript的悬停提示(Eclipse JSDT) - Eclipse plugin development - JavaDoc like hover hint for JavaScript (Eclipse JSDT) 如何在Coldfusion中运行CFGroovy时添加纯javascript编译器插件服务器端? - How to add a pure javascript compiler plugin server-side when running CFGroovy in Coldfusion? ID结束于纯Javascript - ID Ends With in pure Javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM