Advertisement
prasadshir

Core JS Object Example

Jul 31st, 2013
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.41 KB | None | 0 0
  1.  
  2. <!DOCTYPE html>
  3. <html>
  4.     <head>
  5.         <title></title>
  6.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7.     </head>
  8.     <body>
  9.         <div>Object Oriented JS - Work with Console!</div>
  10.         <script>
  11.             /**
  12.             * JavaScript is a prototype-based language which contains no class statement
  13.             * JavaScript uses functions as classes.
  14.             * Defining a class is as easy as defining a function.
  15.             * In the example below we define a new class called Person.
  16.             **/
  17.             function Person(name, gender, age){
  18.             this.name = name;
  19.             this.gender = gender;
  20.             this.age = age;
  21.             };
  22.            
  23.             /*
  24.             * Methods or Member functions are added in the 'propotype' property
  25.             * of the class
  26.             */
  27.            
  28.             Person.prototype = {
  29.             getName: function(){
  30.             console.log(this.name);
  31.            
  32.             },
  33.             getGender: function(){
  34.             console.log(this.gender);
  35.             }
  36.            
  37.             };
  38.            
  39.             Person.prototype.getAge = function(){
  40.             console.log(this.age);
  41.             }
  42.             /*
  43.             * instance of a class is created with 'new' keyword
  44.             * class methods can be called on the instance using a .methodName()
  45.             */
  46.             var p1 = new Person("Prasad","Male",20);
  47.             p1.getName()
  48.            
  49.             /**
  50.             * Inheritance - child classes can be created by simply calling a
  51.             * Parent class in the contructor function
  52.             */
  53.            
  54.             function Student(name, gender, age, school){
  55.             //call parent constructor
  56.             Person.call(this, name, gender, age);
  57.             this.school = school;
  58.            
  59.             }
  60.            
  61.             // inherit Person
  62.             Student.prototype = new Person();
  63.            
  64.             //correct the constructor pointer because it points to Person
  65.                 Student.prototype.constructor = Student;
  66.            
  67.             Student.prototype.getSchool = function(){
  68.             console.log(this.school);
  69.             return this.school;
  70.             }
  71.            
  72.             var s1 = new Student("Shaunak","M",8,"Gurukul" );
  73.             s1.getName();
  74.         </script>
  75.     </body>
  76. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement