0

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 ;)

2
  • 1
    is there a property multi_block inside the pattern object ? Commented Mar 14, 2017 at 8:09
  • yes it is defined there, the problem is with pattern itself undefined not multi_block property inside Commented Mar 14, 2017 at 8:10

2 Answers 2

1

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
    };


}());
Sign up to request clarification or add additional context in comments.

Comments

0

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);

5 Comments

well this obviously will work but it was not the idea to pass entire object as parameter to a function, it should be available globally without need of passing. I am trying to debug more complex problem here.
i'm thinking the problem is the keyword new, which might be creating a new object stack for which local variables of the previous stack are invisible
if you read the code again, pattern is available for use in the create_single method, so why would it be unavailable for create_multi ? thats crazy
from your code, for the variable view you're assigning the return value of a function that returns nothing. So I'm surprised that view.create_single(response); worked to begin with
Currently you altered my code entirely, it was used with keyword new because it is static class, that needs to be accessed from outside, it cannot stand like this, but i solved a problem, it was a structure problem i will print answer in few min.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.