2

Most of the time (tutorials mainly), I have seen "prototype" used for methods.

I'm pretty sure it can also be used for variables (perhaps the most useful for setting "default values"). Is there a reason it's not often used for functions? Is it bad practice, or are there noticeable performance differences?

2
  • are you referring to the prototype framework (as in Javascript framework)? Is there another question that made you ask this? Just curious. What are you ultimately trying to do? Commented Jul 25, 2011 at 22:00
  • @mr-macgyver I'm referring to the built-in static property named "prototype". I'm used to ActionScript and trying to figure out how to "properly" create classes in JavaScript. Commented Jul 25, 2011 at 22:52

3 Answers 3

2

Off the top of my head, you lose the functionality of hasOwnProperty(), which can be useful if you are ever doing a for ... in loop on your object. Consider these two objects:

function MyObject() { ... }
MyObject.prototype.someProperty = 1;

var a = new MyObject();
var b = new MyObject();
b.someProperty = 2;

a.hasOwnProperty("someProperty"); // false
b.hasOwnProperty("someProperty"); // true

That may or may not matter for your situation.

Edit: Thought of another one. If you store objects in your prototype and an instance of that object changes a property on that object, it will affect all other instances:

http://jsfiddle.net/BbmgP/

function MyObject() { ... }
MyObject.prototype.someProperty = { value: 1 };

var a = new MyObject();
var b = new MyObject();

b.someProperty.value = 2;
a.someProperty.value; //  2!! not 1
Sign up to request clarification or add additional context in comments.

1 Comment

Good points. As for the second case, there shouldn't be any problems if you make sure prototype variables contain "simple" data types, rather than references.
1

Think about it the other way round: Why would you want to store an object's attribute in its prototype?

There's two answers:

  1. Save memory. Defining methods on every instance would consume an awful amount, since technically each instance would have its own method. The cost is considerably lower for simple data types.
  2. Shared access. Instances may need to share access to a common attribute. However, this is not possible for simple data types, since those are not stored by reference.

As gilly3 mentioned, 2. may just accidentally happen if you're not absolutely sure what you're doing. (Oh yes, it happened to me...)

Best practice:

  • Methods go to the prototype
  • Attributes go to the instances, unless you need shared access (and the attribute is object-typed).

[edit]

  • Shared access should be implemented using closures, not "class variables"

[/edit]

And yes, the performance of using prototype is slightly worse than the other way. BUT you should never try to optimize things like that, since the performance gain is almost unnoticeable, yet the readability and maintainability of your code may suffer significantly.

Comments

-1

The prototype object of JavaScript, introduced starting in JavaScript 1.1, is a prebuilt object that simplifies the process of adding custom properties/ methods to all instances of an object

You can add default properties, here is an example:

function myObject()
{
    this.constructor();
}

myObject.prototype = 
{

    constructor : function ()
    {
        this.MyProperty1 = "123456"; //default property
        this.MyProperty2 = "bla";    //default property
    }    
};

Let me know if this helps...

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.