Pre Exam Test Practice (SSC | Banking | IAS | GATE | State Level | CLAT | Railways | Insurance | Teaching Exams | NDA | Defence | IES |State Exam )

Organizational Research By

Surprising Reserch Topic

new function with lower case f in javascript

new function with lower case f in javascript  using -'javascript,function,object,instantiation'

My colleague has been using "new function()" with a lower case "f" to define new objects in JavaScript.  It seems to work well in all major browsers and it also seems to be fairly effective at hiding private variables.  Here's an example:

    var someObj = new function () {
        var inner = 'some value'; = 'blah';

        this.get_inner = function () {
            return inner;

        this.set_inner = function (s) {
            inner = s;

As soon as "this" is used, it becomes a public property of someObj.  So, someObj.get_inner() and someObj.set_inner() are all available publicly.  In addition, set_inner() and get_inner() are privileged methods, so they have access to "inner" through closures.

However, I haven't seen any reference to this technique anywhere.  Even Douglas Crockford's JSLint complains about it:

  weird construction. Delete 'new'

We're using this technique in production and it seems to be working well, but I'm a bit anxious about it because it's not documented anywhere.  Does anyone know if this is a valid technique?

asked Oct 11, 2015 by balvant maurya
0 votes

Related Hot Questions

Government Jobs Opening