小编sma*_*333的帖子

jQuery插件开发如何保持变量和方法的私密性

出于练习目的,我正在创建一个jQuery插件,一个简单的图像滑块.我使用Boilerplate中的模式- jQuery插件.

在初始化过程中,一切都按预期工作,每个实例都获得设置所需的正确值(宽度和高度,也包括事件绑定).

当我尝试将计算出的slide-width传递给执行动画的函数(单击下一个按钮)时,麻烦就开始了.我试图保存的每个值都被最后一个实例覆盖 - 好吧,就像我学到的那样,这就是原型继承的作用.

我google了很多,发现(不仅)这个解决方案在stockoverflow:jquery-plugin中的全局变量或局部变量.接受的答案建议存储HTML标记中需要私有的值,data-whatever属性.

这非常好,但不是很好.

第二个答案似乎更优雅,在每个实例的范围内保留真正的私有变量:

(function($) {

$.pluginName = function(element, options) {
....
var plugin = this;
....
        // a public method
    plugin.foo_public_method = function() {
          ....
    }

    // private methods
    var foo_private_method = function() {
          ....
    }
    plugin.init();

} ....
Run Code Online (Sandbox Code Playgroud)

看起来很简单 - 但我无法实现这个解决方案!

所以我的问题是:如何在以下插件模式中实现私有变量?(我如何从Plugin.prototype中正确调用它...)

我用我的插件创建了一个小提琴.代码摘要:

(function ($, window, document, undefined) {

var pluginName = "easyCarousel";

// default configuration object
defaults = {
       ... default values here, …
Run Code Online (Sandbox Code Playgroud)

javascript jquery-plugins private-members prototypal-inheritance

6
推荐指数
1
解决办法
1188
查看次数

将 webpack 配置导出为函数失败

我正在尝试处理 webpack 配置中的开发和生产环境变量(请参阅https://webpack.js.org/guides/product/),但它失败了

WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration should be an object.
Run Code Online (Sandbox Code Playgroud)

包.json

WebpackOptionsValidationError: Invalid configuration object. Webpack has been initialised using a configuration object that does not match the API schema.
 - configuration should be an object.
Run Code Online (Sandbox Code Playgroud)

webpack.config.js

        {
          "scripts": {
            "test": "echo \"Error: no test specified\" && exit 1",
            "build": "./node_modules/.bin/webpack",
            "start": "npm run build && node server.js"
          },
          "devDependencies": …
Run Code Online (Sandbox Code Playgroud)

dev-to-production webpack-4

2
推荐指数
1
解决办法
3907
查看次数