Why is a jquery widget options argument shared across instances?

216 views Asked by At

I had the assumption that all members of options are only accesible inside the widgets instance. I'll give you a example.

(function (factory) {
  //Register as Amd module        
} (function( $ ){

  $.widget('workbench', $basic, {

  options: {
    toolname: undefined,
    tools: [],      
  },

  _create: function(){
    if(this.options.name == undefined){
       alert('Warning, parameter is missing');
       this._destroy();
    }
    this.options.tools.push(this.options.name);
    for(var i =0;  i < this.options.tools.length; i++){
       console.log(this.options.tools[i]);
    }

  },

  });

}));

If i now create instances of the widget workbench

$('<div>').workbench({toolname: 'A'}); 
$('<div>').workbench({toolname: 'B'});

I get the output

A
A
B

But i assumed it should be

A
B

I know that arrays are copied by reference when elements are cloned. But i do not understand why these two instances share the same tools array.

Thank for your help.

0

There are 0 answers