1
(function( $ ) {

    var TagHolder = function(element,options){

        this.property1 = options.p1;
        this.id = 0;

        this.init = function(){
           $('a').on('click',function(e){
              e.preventDefault();

              var id = $(this).attr('id');
              this.id = id;
           });
        }
    }

    $.fn.TagHolderProperty = function(options) {
        return new TagHolder(this, options);
    }
})( window.jQuery );

How can I access this object instance in line this.property1 = options.p1; from line this.id = id; so I can set the id property?

Heero Yuy
  • 614
  • 9
  • 24

2 Answers2

1

Use

self = this;

In your TagHolder function and then do

self.id = id;

In your init function

saurabh singh
  • 419
  • 4
  • 5
0

Store a reference to this in a variable in the parent function, then use self in to reference the parent functions contexts e.g. self.id

(function( $ ) {

    var TagHolder = function(element,options){
       var self = this;


        this.property1 = options.p1;
        this.id = 0;

        this.init = function(){
           $('a').on('click',function(e){
              e.preventDefault();

              var id = $(this).attr('id');
              self.id = id;
           });
        }
    }

    $.fn.TagHolderProperty = function(options) {
        return new TagHolder(this, options);
    }
})( window.jQuery );
Mike
  • 587
  • 3
  • 6