如何将值从静态函数发送到另一个对象?

时间:2019-03-07 14:41:23

标签: javascript jquery class plugins static

  (function ($, window, undefined) {
    'use strict';

    class Slider {

      static init(loop) {
        Slider.loop = loop;
        return Slider.loop;
      }

    };
    $.fn.blueSlider = function(loop, options) {
      var defaults = {

      }
      var loop = Slider.init(loop);
      console.log(loop);
      $.extend(defaults, options);
    };
  }(jQuery))

  $('div').blueSlider(true);
<div></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

我是使用class function编写代码的新手,所以请为我在问题中未提及的其他错误辩解。

我正在尝试结合class function和jQuery插件。这一行:$.fn.blueSlider = function(loop, options),jQuery初始化函数有2个参数,而loop参数实际上是Slider.init()的参数。因此,我必须声明一个类似var loop = Slider.init(loop);的新变量才能访问loop值。

我的期望是将loop参数放在options内,以便当我想像这样更改loop的值时可以由jQuery初始化方法调用:< / p>

$(className).blueSlider({
  loop: false,
  // etc.
})

我尝试创建一个对象options并将其放入loop参数中,以将该值传输到另一个对象,但是它不起作用:

$.fn.blueSlider = function(options) {
  var defaults = {

  }
  var options = {
    loop: Slider.init(loop),
  }
  $.extend(defaults, options);
};

有什么办法可以解决此问题?

1 个答案:

答案 0 :(得分:1)

如果要使用class语法,则不想提供自定义的init函数;而是提供一个构造函数并使用new

请记住,jQuery是基于 set的,这也很重要,这意味着this是一个包含元素 set 的jQuery对象。在大多数情况下,让插件执行的适当操作是好像已分别在每个这些元素上调用它一样。例如:

(function ($, window, undefined) {
    'use strict';
    
    // Moved defaults out of the function, added
    // more for the example
    const defaults = {
      someOption: "foo",
      someOtherOption: "bar",
      color: "green"
    };

    // The class
    class Slider {
        constructor(loop = true, options) {
            // Handle initialization in the constructor
            this.options = defaults;
            this.setOptions(loop, options);
        }
        setOptions(loop, options) {
            this.loop = loop;
            this.options = Object.assign({}, this.options, options);
            // In ES2017+, you could use:
            // this.options = {...defaults, ...this.options};
        }
        // Use a method to handle applying the slider to an element
        apply($el) {
            // ...do slider's thing on $el...
            $el.css("color", this.options.color);
            $el.text("loop = " + this.loop);
        }
    }

    $.fn.blueSlider = function(loop, options) {
        // Loop through the elements
        this.each(function() {
            // Create or reuse an instance for each element
            const $el = $(this);
            let slider = $el.data("blueSlider");
            if (slider) {
                // Element already has a slider
                slider.setOptions(loop, options);
            } else {
                // Create slider for element, remember it
                slider = new Slider(loop, options);
                $el.data("blueSlider", slider);
            }
            slider.apply($el);
        });
    };
}(jQuery))

$('div.a').blueSlider(true, {color: "blue"});
$('div.b').blueSlider(false);
setTimeout(() => {
  $('div.a:first').blueSlider(false);
}, 800);
<div class="a"></div>
<div class="a"></div>
<div class="b"></div>
<div class="b"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>