0

I am trying to use an array of functions into my script. I use namespacing object and when I use this array, all my functions are undefined.

How to create this array with good functions references to process this processAllFunction ?

See my code :

var myns = myns || {};
myns.test = myns.test || {};
myns.test.util = {
    myOne: function(m) {
        return m;
    },
    myTwo: function(m) {
        return m;
    },
    processAllFunction: function(m) {
        for(var i=0; i<this.replaceFilters.length; i++) {
            if(typeof(this.replaceFilters[i])==='function') {
            m= this.replaceFilters[i](m);
        }
        }
        console.log(this.replaceFilters); // undefined functions
        return m;
    },
    replaceFilters: [this.myOne, this.myTwo]
};

2 Answers 2

1

In your code, this either refers to window or to the value of this in the scope of the function where you're defining myns, which is why you're seeing undefined. What you need is a reference to the calling object. You should be able to do it with something like this:

var myns = myns || {};
myns.test = myns.test || {};
myns.test.util = {
    myOne: function(m) {
        return m;
    },
    myTwo: function(m) {
        return m;
    },
    processAllFunction: function(m) {
        for(var i = 0; i < this.getReplaceFilters().length; i++) {
            if(typeof(this.getReplaceFilters()[i]) === 'function') {
               m= this.replaceFilters[i](m);
            }
        }
        return m;
    },
    getReplaceFilters: function() {
        return [this.myOne, this.myTwo];
    }    
};

Notice that instead of replaceFilters simply having the value [this.myOne, this.myTwo], it is now a function that returns [this.myOne, this.myTwo]. This is because when you now call myns.test.util.getReplaceFilters, this points to my.test.util. The same thing happens to this when you call myns.test.util.processAllFunction(); this is again set to myns.test.util. It is this value of this that is also used when you call getReplaceFilters inside processAllFunction.

1
  • Thanks for this explaination, But I cannot modify my filters outside from class/namespace with this method... Commented Jun 28, 2012 at 8:26
0

I found the solution, use replaceFilters outside from object (after declaration).

myns.test.util = {
    myOne: function(m) {
        return m;
    },
    myTwo: function(m) {
        return m;
    },
    processAllFunction: function(m) {
        for(var i=0; i<this.replaceFilters.length; i++) {
            if(typeof(this.replaceFilters[i])==='function') {
            m= this.replaceFilters[i](m);
        }
        }
        console.log(this.replaceFilters);
        return m;
    }
};
myns.test.util.replaceFilters = [this.myOne, this.myTwo];

with this method, I don't have problem with this.

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.