Jump to content

Javascript Prototype Property


Athlon
 Share

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.

Edited by Deirdre's Dad
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...