Home > javascript > Howto: create javascript object without the “new” keyword (quick example)

Howto: create javascript object without the “new” keyword (quick example)

[ Update 2015-02-08 ] Please disregard the “technique” I used in this post. I wrote this post almost 4 years ago when I didn’t have a very deep knowledge about javascript. Kudos to reader Howard for pointing out my mistake.

var Apple=function(type, color) {
    this.type=type;
    this.color=color;
    this.getInfo=function() {
        return "This is a "+color+' '+type+' apple.';
    };
    this.grow=function() {
        console.log('See a '+type+' apple needs to grow too.');
    };
    return this;
};

var myapple=Apple('honeycrip', 'red'); // new Apple ... works too
console.log(myapple.getInfo());
myapple.grow();

The new keyword can be omitted due to the use of return this line (10). Happy coding.

Advertisements
Categories: javascript
  1. vas
    November 25, 2012 at 1:12 am

    This is entirely wrong! What your code above does NOT create a Javascript object, but simply adds type, color and getInfo properties to the global object. If you don’t believe me, trying creating two Apples and after creating both call getInfo on both of them.

    • November 25, 2012 at 8:18 am

      You are right about the result of omitting use of “new” keyword (both objects will return the same result because basically the second Apple() call will overwrite the previous one. Hence the example presented in my post has very limited use – the code will be OK when used in singleton scenario, I am sorry for not pointing that out. But I disagree with you that the properties (type, color, getInfo ) are added to the global object. You can simply try to print the result of this.name at the end of the code (outside function definition) and you should see “undefined” because this inside Apple does not refer to the global object, it refers to the Apple object itself.

      • Howard
        February 4, 2015 at 10:12 pm

        No, you’re still wrong, but that’s ok. (yes i know this is nearly almost 3 years later).

        If we simplify:
        “`
        var Apple = function(color) {
        this.color = color;
        return this; // this doesn’t do anything.
        }
        “`
        this creates a base object. If we ask our runtime what the contents of the `color` variable is, without having written anything else, we’ll get undefined since we haven’t assigned anything to it obviously. if we non-new a new Apple object: `var myapple = Apple(“red”);` then we should be able to ask it what it’s color is: `myapple.color`, getting back the string “red” as you demonstrated above. But if we ask the global object what it’s color is, exactly the same as we did when we got undefined back, we’ll also get the string “red”.

      • February 9, 2015 at 2:26 am

        Yes you are right (and thank you for pointing that out). I wrote that post before I fully understood the keyword `this` in javascript.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: