Introduction
This is the "Advanced JavaScript" article series. In this series, we are learning a few advanced and interesting concepts of JavaScript. So far we have explained various important concepts of this programming language. You can get them here.
- Advance JavaScript: History and role of JavaScript behind modern web
- Advance JavaScript: play with object in JavaScript
- Advance JavaScript: Function Definition Style in JavaScript
- Advance JavaScript: Understand undefined in JavaScript
- Advance JavaScript: Understand "class"-ical concept of JavaScript
- Advance JavaScript: Implement inheritance in JavaScript
- Advance JavaScript: Callback design pattern and callback function in JavaScript
- Advance JavaScript: Exception handling in JavaScript
- Advance JavaScript: Scope of Variable in JavaScript
- Advance JavaScript: closure in JavaScript
In this article, we will learn one more OOP concept of JavaScript called inheritance. As we have explained, JavaScript is an Object Oriented Programming language but is class-less. In other words, there is no concept of a class in JavaScript but we can implement a class by functions and objects. So, let's see how to implement the inheritance concept in JavaScript.
Inheritance in JavaScript
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- function person(name) {
- this.name = name;
- }
- person.prototype.SayName = function () {
- alert("I am " + this.name);
- }
- function student(name) {
- this.name = name;
- }
- //Inherit the property of parent class
- student.prototype = person.prototype;
- var s = new student('Sourav Kayal');
- s.SayName();
- </script>
- </form>
- </body>
- </html>
- function person(name) {
- this.name = name;
- }
- person.prototype.SayName = function () {
- alert("I am " + this.name);
- }
- function student(name) {
- this.name = name;
- }
- //Inherit the property of parent class
- student.prototype = person.prototype;
- var s = new student('Sourav Kayal');
- s.SayName();

- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="JavaScript.aspx.cs" Inherits="JavaScript.JavaScript" %>
- <!DOCTYPE html>
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head runat="server">
- </head>
- <body>
- <form id="form1" runat="server">
- <script>
- function father(name) {
- this.name = name;
- }
- father.prototype.SayName = function () {
- alert("Name is:- " + this.name);
- }
- function mother(name) {
- this.name = name;
- }
- function son(name) {
- this.name = name;
- }
- mother.prototype = father.prototype;
- son.prototype = mother.prototype;
- var s = new son('Sourav Kayal');
- s.SayName();
- </script>
- </form>
- </body>
- </html>


Sourav KayalPosted Dec 5, 2013, 11:38 AM
In prototype property of person class(in JavaScript it's function) we are attaching one property called SayName which will contain one anonymous function.
Former memberPosted Dec 5, 2013, 8:32 AM
hi go through the article but do not understand the role of this line of code. person.prototype.SayName = function () { alert("I am " this.name); } please tell me what is the meaning of this line. thanks