0

here is my javascript:

function namespace(nameSpaceString){
    var parts = nameSpaceString.split(".");
    var parent = window;
    var currentParent = '';

    for(var i=0, len=parts.length; i<len;i++){
        currentParent = parts[i];
        parent[currentParent] = parent[currentParent] || {};
        parent = parent[currentParent];
    }

    return parent;
}
abc = namespace('ns.abc');

abc.common = function(){
    var arr = [];

    setArr = function(v){
        if(v){
            arr.push(v);
        }
    }
    getArr = function(){
        return arr;
    }

    registerArr = function(ns){
        if(ns){
            for(var obj in ns){
                if(obj.hasOwnProperty(getName)){
                    setArr(obj.getName());
                }
            }
        }
    }
    return{
      setArr : setArr,
      getArr : getArr,
      registerArr : registerArr
   }
}

abc.form = function() {
var salutation = "Hi";
var name = '';

getSalutation = function(){
    return salutation;
}
getName = function(){
    return name;
}
setSalutation = function(s){
    salutation = s;
}
setName = function(n){
    name = n;
}

return{
    getSalutation:getSalutation,
    setSalutation:setSalutation,
    getName : getName,
    setName : setName
    }
}

persons = namespace('ns.abc.persons');
persons.Dave = new abc.form();

persons.Dave.setName("Dave");

persons.Mitchell = new abc.form();

persons.Mitchell.setName('Mitchell');
persons.Mitchell.setSalutation('Howdy');

alert(persons.Mitchell.getSalutation()+":"+persons.Mitchell.getName());
commonObj = new abc.common();

commonObj.registerArr(persons);
alert("Registration:"+commonObj.getArr())

commonObj.setArr(persons.Dave.getName());
commonObj.setArr(persons.Mitchell.getName());
alert("Setter Methods:"+commonObj.getArr());

Here the Arr in Common when set by setter method works fine. But when I try to achieve the same by calling setter method form another member function -"registerArr" of the same object, it returns nothing.

How can I use setter method from within another member function?

2
  • here is the code to play around -- jsfiddle.net/UdyYv/1 Commented Aug 10, 2011 at 19:57
  • your for.. in loop is wrong! Work that out and your problem will be solved. (Use normal iteration.. no need to complicate things). Also, you shouldn't be creating so many globals! use 'var' its your friend! Commented Aug 10, 2011 at 20:16

2 Answers 2

2

You need to declare the variables holding the functions using var.

Since you aren't declaring them, the variables become globals.

Therefore, registerArr will always use the functions from the last instance created, since that's what's in the global variables.

Also, you're misusing the for... in loop.
obj iterates over the keys of every property in the object.
To iterate over an array, use a normal for loop.

Sign up to request clarification or add additional context in comments.

1 Comment

declaring the properties using var works fine. However if I were to use "this" inside registerArr i.e. this.setArr(), that will still point to the locally declared method of that particular instance of common object object, right?
0

There are two issues with your for..in loop:

  1. obj refers to the key not the value. Get the value by using ns[obj].
  2. The argument for .hasOwnProperty() needs to be a string. Add quotes.

Here is the corrected loop:

for (var obj in ns) {
    if (ns[obj].hasOwnProperty("getName")) {
        setArr(ns[obj].getName());
    }
}

Here is a working jsFiddle: http://jsfiddle.net/UdyYv/10/

Besides that, there were some other problems in your jsFiddle that caused it to fail, throwing errors when run. You needed to declare namespace.abc and namespace.abc.person.

This turned out to be a red herring. Because you are instantiating instances of abc.common using the new keyword, abc.common needs to specify methods, rather than returning an object. Remove the return statement and replace with this:

this.setArr = setArr;
this.getArr = getArr;
this.registerArr = registerArr;

If you don't want to use this approach, than you should not instantiate it using the new keyword. Instead just call it:

commonObj = abc.common();

2 Comments

how can I get more than one instances of the same object if I dont declare using the key word new? Look at the persons object, it follows the same return pattern as common object and it works fine for Dave and Mitchell objects
@user - Your function is not returning a reference to a singleton object, it's returning an object literal. Every time you call abc.common() you get a new unique object. As for the new keyword, I see that it does indeed still work with it, which surprises me a little. It turns out the real issue with your code is the two problems in your for..in loop, which I added to my answer.

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.