0

I have this javasctipt class

function employee(name, jobtitle, born)
{
    this.name = name;
    this.jobtitle = jobtitle;
    this.born = born;
}

var fred = new employee("Fred Flintstone", "Caveman", 1970);
employee.prototype.salary = null;
fred.salary = 20000;
fred.notprototype = 1239;
console.log(fred);

now as you can see I added salary property using prototype but then I just added a property by using fred.notprototype = 1239; without the use of prototype.

when I did the console.log on object fred I see notprototype there. So Is it wrong not to add the prototype? If it is then what difference is it making?

Jivings
  • 22,834
  • 6
  • 60
  • 101
Asim Zaidi
  • 27,016
  • 49
  • 132
  • 221
  • 1
    [Introduction to Object-Oriented JavaScript](https://developer.mozilla.org/en/Introduction_to_Object-Oriented_JavaScript) – Jivings May 20 '12 at 16:15

2 Answers2

3

Let's say, after all that code you add this:

var john = new employee("John Carson", "Philantropist", 2015);
console.log(john);

It will show that john has a salary attribute (of value null). You didn't set that yourself, it comes from the prototype chain.

By adding to a prototype of X, you change all objects that are created by new X().

Another example:

function School(name)
{
    this.name = name;
}

School.prototype.getName = function() {
    return this.name;
}

var school_one = new School('one');

console.log(school_one.getName()); // prints "one"

In this example, a method getName was added to the prototype. Upon creation of the school_one object it inherits from School.prototype all that you have added to it (in this case just that one method).

This can be used to declare the object's interface (methods you use to access it) or default values separate from the constructor.

To get a deeper understanding of JavaScript and its unique features, I highly recommend checking out a few videos from Douglas Crockford; he's a great speaker and highly knowledgeable, love listening to him :)

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • umm, no - when you add something to the prototype it _immediately_ becomes accessible in _all_ objects of that type. – Alnitak May 20 '12 at 16:17
  • By adding to a prototype of X, you change all objects that are created by new X() from that point onwards. could you give an example of that? I am kinda confused on that – Asim Zaidi May 20 '12 at 16:20
  • @Alnitak ah right, updated that; he added the `.salary` attribute before adding the same attribute to the prototype, so my brain waves got confused ;-) thanks! – Ja͢ck May 20 '12 at 16:20
  • @Autolycus I've highlighted that by showing the `john` example; it will have a `.salary` attribute, because you added it to the prototype (prototypes are not like normal attributes, they have a special meaning in JavaScript) – Ja͢ck May 20 '12 at 16:22
  • @Autolycus I've updated the answer, hope that makes it somewhat clearer – Ja͢ck May 20 '12 at 16:31
1

Properties on the prototype do two things:

  1. for "plain" variables, they can provide a default value, which will be hidden the first time you write a property with the same name to an object

  2. for member functions, they reduce the overhead of every instance of the object containing its own copy of that function.

Alnitak
  • 334,560
  • 70
  • 407
  • 495