1

Is it possible to add properties to a function, without using prototype? I know with the prototype you can do things like:

function Gadget(name, color) { 
    this.name = name; 
    this.color = color; 
    this.whatAreYou = function(){ 
    return 'I am a ' + this.color + ' ' + this.name; 
    }
}

Can this same goal be accomplished without the prototype object?

4
  • 7
    Prototypes are not being used here. Commented Nov 3, 2013 at 1:02
  • Just make sure to use new when you instantiate that "class" or this will be bound to the outermost scope. Commented Nov 3, 2013 at 1:05
  • Note that you're not adding properties to a function, you're adding properties to the object that would be created if you called the function with new Gadget(). Commented Nov 3, 2013 at 1:12
  • ooo Thanks for the correction. Commented Nov 3, 2013 at 1:22

2 Answers 2

1

There's a little bit of confusion about what you're asking. You are currently not using the prototype (couldn't tell from your question whether you realized that or not) for your methods or properties and that technique works just fine if you create an object from the function with new like this:

function Gadget(name, color) { 
    this.name = name; 
    this.color = color; 
    this.whatAreYou = function(){ 
        return 'I am a ' + this.color + ' ' + this.name; 
    }
}

var x = new Gadget("Faucet", "red");
x.whatAreYou();   // returns 'I am a red Faucet'

Working demo: http://jsfiddle.net/jfriend00/wPP7N/

When the new operator is used, it creates a new object and calls the function with this assigned to the new object. Any properties you add to the object that this points to in the constructor then become properties of the new object.

In fact, properties with a dynamic value like name and color in your example are often assigned like this in the constructor as there is little advantage to using the prototype for them because they get assigned a dynamic value. There is a performance advantage to assigning methods such as your whatAreYou method using the prototype because less code has to run at constructor time - though the difference is not huge.


To compare and contrast, code using the prototype to define the method would look like this:

function Gadget(name, color) { 
    this.name = name; 
    this.color = color; 
}

Gadget.prototype.whatAreYou = function(){ 
    return 'I am a ' + this.color + ' ' + this.name; 
}

var x = new Gadget("Faucet", "red");
x.whatAreYou();   // returns 'I am a red Faucet'

If you just call the function plainly like:

Gadget();

Then, there is no new object created and this will either point to the global object or will be undefined (in Strict mode) so the properties would not be on a Gadget-specific object.

Sign up to request clarification or add additional context in comments.

Comments

0

See the comments to your question (you aren't actually using prototype), but just to help you understand, using prototype would look like this:

function Gadget(name, color) {
  this.name = name; 
  this.color = color; 
}

Gadget.prototype.whatAreYou = function(){
  return 'I am a ' + this.color + ' ' + this.name; 
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.