0
function Cons()
    {
        this.a = "variable a";
        this.callme = function callme()
        {
            var str = "";
                for(var x in this)
                {
                    str += this[x] + " ";
                }
            return str;
        }
    }

    var obj = new Cons();

    obj.c = "variable 2";

    var fin = obj.callme();
    document.write(fin);

I want to have a function inside the object so that when it is called it can return a string consisting of the values of every member. Here in this case a and c. Now what happens, everything inside the function i mean the code is printed inside the browser instead of just returning the value of str.

What i understood is that this["callme"] part in the for-in loop returns the whole code as its also a variable. So how to solve this problem.

I am new to javascript please help me.

3
  • What have you researched/tried so far ? I don't see any attempt of looping in the code you posted... Commented Nov 12, 2013 at 17:39
  • there is a for loop inside the callme function that creates all the problem but thats what i was trying to achieve Commented Nov 12, 2013 at 17:41
  • you can skip functions when you loop. if this is what you want. Commented Nov 12, 2013 at 17:42

2 Answers 2

2

There are several ways to solve this:

1) Remove callme from the for..in :

for(var x in this) {
  if (x !== 'callme') {
    str += this[x] + " ";
  }
}

2) Declare callme as a non-enumerable property:

function Cons() {
    Object.defineProperty('callme', {
      enumerable : false,
      value : function callme() {
        var str = "";
            for(var x in this)
            {
                str += this[x] + " ";
            }
        return str;
      }
    });
}

3) Change the toString method of callme to return nothing:

function Cons() {
    this.a = "variable a";
    this.callme = function callme() {
        var str = "";
            for(var x in this)
            {
                str += this[x] + " ";
            }
        return str;
    }
    this.callme.toString = function(){ return '';};
}

The simplest solution is the first one, but the others are too interesting to pass.

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

2 Comments

Thank you for helping. I really need to study more to understand the last two as i said i am new to js. But didn't the 1st one will only just skip the callme function instead of skipping all the other functions inside the object?
Yes. @matewka gave you a good answer on skipping all the functions, not just callme.
1

If you want to avoid the function body being printed, check if the property is a function and print only its name:

this.callme = function callme()
    {
        var str = "";
            for(var x in this)
            {
                if ('function' === typeof this[x]) {
                    str += x + " ";
                } else {
                    str += this[x] + " ";
                }
            }
        return str;
    }

3 Comments

Hey thanks a lot. Thanks a lot. This is what i was looking for. Can you please tell me a good guide from where can i learn more about functions and objects of javascript.
Mozilla Developer Network (MDN) is THE best place to study things about JavaScript and frontend stuff in general.
@Tibos, + for you. Here's the link MDN. As an addition - try to avoid w3schools as they're no longer a w3 branch and sometimes provide some misleading information.

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.