-3

Is there any point of using the Prototype function for a an object in JavaScript if you have no interest in using inheritance for that object?

Put another way, does the Prototype function have any purpose other than to achieve a mechanism of inheritance?

Breako Breako
  • 6,353
  • 14
  • 40
  • 59
  • 8
    Is there any reason to use JavaScript if you have no interest in using JavaScript? – user229044 Oct 13 '13 at 14:27
  • 1
    I'm assuming there's a typo in here given your second question – Hassan Hayat Oct 13 '13 at 14:28
  • Did you mean “inheritance” instead of “JavaScript”? – Ry- Oct 13 '13 at 14:56
  • Prototype is a shared object for all instances you create, if you have a function Person and use it to create persons (`ben=new Person("Ben");`) Instance specific value of this person would be the name Ben. You would not want that on the prototype because everyone would be named Ben (until you override it on the instance). Functions however do pretty much the same thing for every Person instance so there is no point for every instance to have it's own. You typically share one function on the prototype for every instance. See intro here for more http://stackoverflow.com/a/16063711/1641941 – HMR Oct 14 '13 at 04:11
  • 1
    By defining shared members on the prototype you initialize your object faster and use less memory to create them. It's easier to test these objects because you can override functions on the prototype once with mock functions and then create as many instances as you need without having to mock specific instance functions again. – HMR Oct 14 '13 at 04:14
  • I consider this question valid.. since a prototype can be used for multiple instantiation of the same class, without inheritance/ subtyping needing to apply. Voting to re-open. – Thomas W Nov 02 '13 at 01:20

2 Answers2

2

...does the Prototype function have any purpose other than to achieve a mechanism of inheritance?

I assume you mean the prototype property of Function objects. (There is no "Prototype function" in JavaScript.)

It depends on what you mean by "inheritance." The purpose of the prototype property on Function instances is to specify the object that will be assigned as the prototype of objects created using that function as a constructor, e.g.:

var f = new Foo(); // Assigns `Foo.prototype` as `f`'s prototype

That way, f has access to the properties defined by that prototype. If that's what you mean by inheritance, then yes, that's the only purpose for the prototype property on functions.

Note that now we have ECMAScript5, you can assign prototypes without using constructor functions, if you prefer, using the new Object.create:

var f = Object.create(somePrototype); // Assigns `somePrototype` as `f`'s prototype

Here's a more concrete example of both of those:

Using a constructor function:

function Foo(name) {
    this.name = name;
}
Foo.prototype.hey = function() {
    console.log("Hi, I'm " + this.name);
};
var f = new Foo("Fred"); // Assigns `Foo.prototype` as `f`'s prototype
f.hey();                 // Shows "Hi, I'm Fred" because `f` gets `hey` from its prototype

Using Object.create:

var fproto = {
    hey: function() {
        console.log("Hi, I'm " + this.name);
    }
};
var f = Object.create(fproto); // Assigns `fproto` as `f`'s prototype
f.name = "Fred";
f.hey();                       // Shows "Hi, I'm Fred" because `f` gets `hey` from its prototype

So if you prefer only to use Object.create, and you're only using ES5-enabled engines, you could never use Function#prototype at all if you like.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Unlike classical inheritance (Like Java) it allows you to access the parent's attributes.

For instance:

var criminal = {
           'name': 'John',
           'crime': 'Murder'
           }
criminal.name; // 'John'
var criminal2 = Object.create(criminal);
criminal2.prison = 'Famous Prison';
criminal2.name; // 'John'
criminal2.name = 'Tom';
criminal2.name; // 'Tom'

If that makes any sense. I might be wrong about the Java comparison though.

Also, you are thinking of JavaScript as if it were another language. And it isn't. It's JavaScript, it does not have classes or inheritance it is a prototype-kind-of-language.

You should look at JavaScript: The Good Parts by Crockford.

iamricard
  • 483
  • 7
  • 16