Jump to content

Javascript Prototype Property


Athlon

Recommended Posts

problem here is understand nearly nothing... first of all i want you to explain that stringi readen the below code but still cant understand it :)

<script type="text/javascript">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;document.write(fred.salary);</script>

Link to comment
Share on other sites

If you do not understand how objects are created, my explanation won't mean very much. You should understand that before you think about the prototype property.The prototype property allows you to modify the structure of every instance of an object, whether the object was created before you modified the structure or after. In the example, fred is created by the object constructor employee. Then the prototype is modified. fred now has a salary property, and every other object constructed by employee will have a salary property.This is different from giving only fred a salary property.The example is a little dumb, because the salary property is initialized to null. If the code looked like this:

employee.prototype.salary=2000;alert(fred.salary);

the result would be the same. fred now has a salary property, and the value is set to 2000. Every object created by employee has a salary property set to 2000.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...