Skip to content

A simple explanation of “new” in Javascript.

by Qard on April 10th, 2011

There is a major feature of Javascript that is sorely misunderstood–the “new” keyword. Using the “new” keyword when declaring a variable will assign it’s value to the state of “this” upon completion of the function. Not using “new” will simply assign it’s value to the return value, or ‘undefined’ if nothing was returned. I wrote a simple example below to illustrate the effects of using the new keyword.

function Test(name){
    this.test = function(){
        return 'This will only work through the "new" keyword.';
    }
    return name;
}

var test = new Test('test');
test instanceof Test; // returns true
test.test(); // returns 'This will only work through the "new" keyword.'
test // returns the instance object of the Test() function.

var test2 = Test('test');
test2 instanceof Test; // returns false
test2.test(); // throws TypeError: Object #<Test> has no method 'test'
test2 // returns 'test'

As Andrew kindly pointed out; if you return an object the resulting value will be an instance of the returned object rather than the constructor.

function Test(name){
    this.test = function(){
        return 'This will only work through the "new" keyword.';
    }
    return {name:name};
}

var test = new Test('test');
test instanceof Test; // returns false
test2.test(); // throws TypeError: Object #<Test> has no method 'test'
test // returns {name:'test'}.
Be Sociable, Share!
  • Andrew

    One small tweak – if the constructor returns an object then it will be returned instead of ‘this’.

    e.g. change “return name” to “return {a:12}” and you will find:

    var test = new Test(‘test’);
    test instanceof Test; // returns false

  • chadams

    I have been thinking about this a little. Perhaps an even simpler way to explain…
    using the “new” keyword before a function automatically adds “return this;” at the end of the function if no return already exists. This way of thinking helps eliminate the confusing weird behavior “new” seems to have.

  • http://fireyfly.myopenid.com/ Jonas Höglund

    Not necessarily; if you attempt to return anything but an object (for example a number), it’ll still evaluate to the newly constructed object. `function Foo() {return 42}; (new Foo) instanceof Foo` evaluates to true.

    The best way to understand how `new` works would be to read the spec, but it can be a bit difficult to understand sometimes. However, if you actually want to know for sure how it works, I’d suggest checking there: http://es5.github.com/#x13.2.2

    8. Let result be the result of calling the [[Call]] internal property of F, providing obj as the this value and providing the argument list passed into [[Construct]] as args.
    9. If Type(result) is Object then return result.
    10. Return obj.

  • http://fireyfly.myopenid.com/ Jonas Höglund

    While we’ve already touched `instanceof` it might be worth mentioning that it’s actually nothing more than a lookup in the prototype chain; basically, `a instanceof b` checks if the object a has b.prototype as its prototype, or if it’s the “prototype of the prototype”, and so on.

    This could lead to subtle problems, as in the following:

        function Test() {}
        var test = new Test()
        test instanceof Test // true
        
        Test.prototype = {foo: 42};
        test instanceof Test // false
        test.foo // undefined (since Test.prototype doesn’t refer to the [[Prototype]] of test)

  • Jon H