Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Подскажите по созданию плагина (https://javascript.ru/forum/jquery/70717-podskazhite-po-sozdaniyu-plagina.html)

elink12 28.09.2017 00:44

Да, из init проходит, а вот если из log вызвать log1 то получаю ошибку через this = Uncaught TypeError: this.log1 is not a function

Plugin.prototype = {
    init: function (e) {
      $(this.element).click($.proxy(this.click, this));
      this.log('test');
    },
    click : function(e){
      e.target.value = this.options.color;
    },
    log: function(msg) {
      console.log(msg);
      this.log1('test');
    }
    log1: function(msg) {
      console.log(msg);
    }
};


Цитата:

Сообщение от Rasy (Сообщение 465774)
Да, объект события подходит для манипуляций.

Просто обратиться к методу объекта
Plugin.prototype = {
    init: function (e) {
      $(this.element).click($.proxy(this.click, this));
      this.log('test');
    },
    click : function(e){
      e.target.value = this.options.color;
    },
    log: function(msg) {
      console.log(msg);
    }
};


рони 28.09.2017 00:50

elink12,
строка 12, запятая где?

elink12 28.09.2017 01:50

Ночь, улица, фонарь, запятую забыл :) но это сути не поменяло. На jsfiddle создал , так же в консоли ошибка https://jsfiddle.net/9dykvku6/8/

<div id='alert'>
1.<input type="text">
</div>
<div id='alert1'>
2.<input type="text">
</div>

(function($) {


    var pluginName = 'Plugin',
        defaults = {
            color: "green",
        },
        options = {};


    function Plugin( element, options ) {
        this.element    = element;
        this.options    = $.extend( {}, defaults, options) ;
        this.defaults   = defaults;
        this.name       = pluginName;
        this.input_name = $(element).attr('name');
        this.init();
    }

    Plugin.prototype = {
        init: function (e) {
           $(this.element).on({"keyup" : this.log},"input",this.options);
        },
        log: function(msg) {
           console.log(msg);
           this.log1('test');
        },
        log1: function(msg) {
           console.log(msg);
        }
    };

    $.fn.Plugin = function ( options,event ) {
      return  this.each(function () {
           new Plugin( this, options );

        });



    }
})(jQuery);
$( document ).ready(function() {
   $('#alert').Plugin({color:'ааа'});
   $('#alert1').Plugin({color:'red'});
});

рони 28.09.2017 03:22

elink12,
$(this.element).keyup($.proxy(this.log, this))

или
$(this.element).on({"keyup" : this.log.bind(this)},"input",this.options);


только у вас msg === event в log


Часовой пояс GMT +3, время: 10:33.