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';
        this.foo = '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.foo, 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
9 views



Related Hot Questions

4 Answers

0 votes

I've seen that technique before, it's valid, you are using a function expression as if it were a Constructor Function.

But IMHO, you can achieve the same with an auto-invoking function expression, I don't really see the point of using the new operator in that way:

var someObj = (function () {
    var instance = {},
        inner = 'some value';

    instance.foo = 'blah';

    instance.get_inner = function () {
        return inner;
    };

    instance.set_inner = function (s) {
        inner = s;
    };

    return instance;
})();

The purpose of the new operator is to create new object instances, setting up the [[Prototype]] internal property, you can see how this is made by the [Construct] internal property.

The above code will produce an equivalent result.

answered Oct 11, 2015 by tushar2k6
0 votes

Your code is just similar to the less weird construct

function Foo () {
    var inner = 'some value';
    this.foo = 'blah';

    ...
};
var someObj = new Foo;
answered Oct 11, 2015 by suyesh.lokhande
0 votes

To clarify some aspects and make Douglas Crockford's JSLint not to complain about your code here are some examples of instantiation:

1. o = new Object(); // normal call of a constructor

2. o = new Object;   // accepted call of a constructor

3. var someObj = new (function () {  
    var inner = 'some value';
    this.foo = 'blah';

    this.get_inner = function () {
        return inner;
    };

    this.set_inner = function (s) {
        inner = s;
    };
})(); // normal call of a constructor

4. var someObj = new (function () {  
    var inner = 'some value';
    this.foo = 'blah';

    this.get_inner = function () {
        return inner;
    };

    this.set_inner = function (s) {
        inner = s;
    };
}); // accepted call of a constructor

In example 3. expression in (...) as value is a function/constructor. It looks like this: new (function (){...})(). So if we omit ending brackets as in example 2, the expression is still a valid constructor call and looks like example 4.

Douglas Crockford's JSLint "thinks" you wanted to assign the function to someObj, not its instance. And after all it's just an warning, not an error.

answered Oct 11, 2015 by vijayshukla80
0 votes

Like Crockford says, it's weird. See CMS's answer. But, to answer explicitly, what you are doing is fine, just weird. Please read the ECMAScript spec. The latest version is here: http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf Yes, it's hard work, but it's worth the effort. It might be heavy going but it's not meant to be read cover-to-cover. Just pick out the bits you need and follow any cross-references. Each time you look at it it will become easier to understand.

answered Oct 11, 2015 by deepak

...