0

Long ago I've read a lot about javascript coding conventions, and one of the things I wanted to decide was the better way of declaring functions. I finally somehow stuck with the

var func_name = function(){};

version, because it seemed more useful in certain scenarios, but I wasn't really able to find things that wouldn't work the same way between them until now.

I was writing a function that should've returned a new function to be used in a setTimeout command, but I couldn't get it to work properly, and after I reduced the whole thing to this test code:

var test = new function(x) {
  return function() {
    if (x % 2 == 1) {
      console.log('a');
    }

    else {
      console.log('b');
    }

    x++;
  };
};

I happened to try if writing it in the

function func_name(){}

style would help (because I really couldn't see the problem with my code), and interestingly enough this code

function test(x) {
  return function() {
    if (x % 2 == 1) {
      console.log('a');
    }

    else {
      console.log('b');
    }

    x++;
  };
}

seems to be working perfectly. A weird thing to discover was that after playing a bit around in the console I realized that the first one effectively becomes the function it should generate.

I tested it in Chrome and Firefox too, and I also tried using it this way

var test = new function(x) {
  var result = function() {
    if (x % 2 == 1) {
      console.log('a');
    }

    else {
      console.log('b');
    }

    x++;
  };

  return result;
};

but I wasn't able to make it work.

I would be interested in any explanation to this phenomenon and also it fascinates me if there is a way to make this type of function declaration capable of producing functions.

Thanks in advance!


Edit: I don't know how, but somehow that new keyword got there by mistake :D (and even into the third version by that stupid copy-paste laziness of mine.....) I'm still interested in knowing why the function becomes what it should create though!

2
  • 1
    Don't use new, just use var test = function() ... Commented Jun 7, 2014 at 17:31
  • Thanks!!! I don't know how I could overlook that... (I didn't intend to write it there, I don't know how that happened XD) I'm still interested in knowing why it becomes what it should generate though1 Commented Jun 7, 2014 at 17:32

1 Answer 1

2

Why are you using new? Remove that and it should be fine IMO.

You are using it as if it were a Construtor.

Though valid, can create issues as your current issue.

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

2 Comments

Thank you very much! I don't know how I could overlook that... (it was kinda just like a typo, I've never messed this up before)
glad to have helped you :)

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.