Mevia
Mevia

Reputation: 1564

javascript variable undefined (out of scope)

I have a odd problem with reference to a variable. My setup is following:

var pattern = {/* some object with patterns */};

var view = (new function() {

    this.create_single = function(response) {
        pattern.block // this returns pattern object correctly
    };

    this.create_multi = function(response) {
        pattern.multi_block // this returns pattern as undefined
    };

}());

var data = (new function() {

    this.acquisition = function(response) {
        view.create_single(response);
        view.create_multi(response);
    };

}());

So in the create_multi method pattern variable returns undefined and i don't have any clue why its happening. In Adobe Dreamweaver (which i am using to write code) i have a line error that says 'pattern' used out of scope. Can anyone help me understand what is happening ?

Thank You for all the help ;)

Upvotes: 0

Views: 87

Answers (2)

Hanif
Hanif

Reputation: 3795

Your approach is okay but your 'view' method's returning nothing. It is working fine with following scenario:

var pattern = {
    block: 1,
    multi_block: 2
};

var view = (new function() {

    this.create_single = function(response) {
        return pattern.block // this returns pattern object correctly
    };

    this.create_multi = function(response) {
        return pattern.multi_block // this returns pattern as undefined
    };


}());

Upvotes: 1

xxfast
xxfast

Reputation: 737

Try this one without parameter passing

var pattern = {multi_block:"mblock", block:"block" };

var view = ( function() { 
   var create_single = function(response) {
      return pattern.block // this returns pattern object   correctly
   };
   
   var create_multi = function(response) {
    return  pattern.multi_block // this returns pattern as undefined
   };
   return create_single;
}());

console.log(view);

Upvotes: 0

Related Questions