Quantcast
Channel: Posts/rss
Viewing all articles
Browse latest Browse all 38

Comment from Rubiwachs

$
0
0
I was not looking for the code itself :), I am only interested in the structure of the code => 3 objects in the same namespace, accessing common values. Would you do it like this:

if (typeof(MyNamespace) == 'undefined') var MyNamespace = {};

MyNamespace.MyObject1 = function() {
  var util = {
    _getValue: function() { return MyNamespace.value; },
    _setValue: function(value) { MyNamespace.value = value; }
  }

  var obj = {
    init: function() {},
    getValue: function() { return util._getValue(); },
    setValue: function(value) { util._setValue(value); }
  }
	
  obj.util = util;
  return obj;
}();

MyNamespace.MyObject2 = function() {
  var util = {
    _getValue: function() { return MyNamespace.value; },
    _setValue: function(value) { MyNamespace.value = value; }
  }

  var obj = {
    init: function() {},
    getValue: function() { return util._getValue(); },
    setValue: function(value) { util._setValue(value); }
  }
	
  obj.util = util;
  return obj;	
}();


// set currentValue via MyObject1
MyNamespace.MyObject1.setValue('test1');

// retrieve currentValue via different objects
alert(MyNamespace.MyObject1.getValue());
alert(MyNamespace.MyObject2.getValue());
Both alert's will return 'test1'. Any thoughts?

Viewing all articles
Browse latest Browse all 38

Trending Articles