JS Secrets: OOP in JavaScript Lesson 2


SUBMITTED BY: Guest

DATE: Dec. 30, 2013, 5:29 a.m.

FORMAT: JavaScript

SIZE: 1.4 kB

HITS: 1617

  1. //Now Lets do some Inheritance
  2. //First create a basic type: Person
  3. function Person(firstName, lastName, birthDate)
  4. {
  5. this.FirstName = firstName;
  6. this.LastName = lastName;
  7. this.BirthDate = birthDate;
  8. }
  9. //Now we can use this type:
  10. var myPerson = new Person("Steve", "Miller", "06.20.1978");
  11. var myPersonFirstName = myPerson.FirstName;
  12. var myPersonLastName = myPerson.LastName;
  13. var myPersonBirthDate = myPerson.BirthDate;
  14. //Now lets create a new type: Employee
  15. //An Employee is a person so it can inhertit its propertys
  16. function Employee(firstName, lastName, birthDate, position, employeeID)
  17. {
  18. //Inherit from Person
  19. Person.call(firstName, lastName, birthDate);
  20. //But we need new propertys that person does not contain
  21. this.Position = position;
  22. this.EmployeeID = emloyeeID;
  23. }
  24. //Now lets create a new Employee
  25. var myEmployee = new Employee("Steve", "Miller", "06.20.1978", "Boss", 1);
  26. //Employee hast every property of a Person:
  27. var myEmloyeeFirstName = myEmployee.FirstName;
  28. var myEmployeeLastname = myEmployee.LastName;
  29. var myEmployeeBirthDate = myEmployee.BirthDate;
  30. //And Also its own propertys
  31. var myEmloyee.Position = myEmployee.Position;
  32. var myEmployeeID = myEmployee.EmployeeID;

comments powered by Disqus