4

http://jsbin.com/ufihev/3/edit

Pretty simple code :

var t = new function () //line 1
    {
        this.a1 = function () {

            return function () {
                alert("1");
            };
        }();
        this.a2 = function () {
            alert("a2");
        };

    }; //line 16
t.a1();

But jsBin red line bellow tells me :

Line 1: var t = new function () --- Weird construction. Delete 'new'.

Line 16: }; --- Missing '()' invoking a constructor.

the code is working fine.

What am I doing wrong ?

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • Working fine in my Firefox/Chrome. Which browser are you running trouble into? – Abdul Munim Dec 10 '11 at 09:34
  • @Munim jsBin red line bellow .....Ive edited. look at the link . thanks. – Royi Namir Dec 10 '11 at 09:35
  • @Munim it's working with two warnings. – Rupesh Pawar Dec 10 '11 at 09:36
  • See this question: http://stackoverflow.com/questions/2274695/new-function-with-lower-case-f-in-javascript – Hristo Dec 10 '11 at 09:42
  • @Hristo How the hell did you find this ??? :) thanks I will look at that. – Royi Namir Dec 10 '11 at 09:42
  • @RoyiNamir Your code is correct. However jsbin uses jslint. And jslint says "this code is silly". Thus it flags a warning. I recommend you don't use `new function () { }` because there are better alternatives. – Raynos Dec 10 '11 at 09:45
  • @Hristo Still I cant understand why my code is BAd..... I've read the link answer , and the solution was to create an inside instance in autoexecute the wrapper function... thats fine also.. but I want to know why my code is bad – Royi Namir Dec 10 '11 at 09:46
  • @Royi... I found it by doing a simple Google search. Here's another one http://stackoverflow.com/questions/6440193/constructing-object-with-new-function-vs-invoking-function-with-function... but I can't exactly tell you why its bad. I'm not saying it is bad or good, it just seems that `new function() {}` is not common practice. Plus, if jsBin uses jsLint and jsLint was written by Douglas Crockford and it tells you to not do that, then I would listen because Douglas Crockford is the JavaScript guru. – Hristo Dec 10 '11 at 09:58

2 Answers2

2

If you're going to write an "anonymous type" equivalent to:

var _anonymous_type = function () {
    ...
}

var t = new _anonymous_type();

You should:

var t = new (function () {
    ...
})();

See the difference from you code? :)

Jeffrey Zhao
  • 4,923
  • 4
  • 30
  • 52
1

Try this:

var t = function () //line 1
    {
        this.a1 = function () {

            return function () {
                alert("1");
            };
        }();
        this.a2 = function () {
            alert("a2");
        };

    }; //line 16

var x = new t();
x.a1();
regality
  • 6,496
  • 6
  • 29
  • 26
  • 1
    I can do that ALSO.... But my Code is syntactical fine. I can't see any reason why it wont work.... – Royi Namir Dec 10 '11 at 09:39
  • Technically there is nothing wrong with `var t = new function()...`, but there isn't a good reason to do that. Hence the *weird* constructor. – regality Dec 10 '11 at 09:53