473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

is polymorphism supported by javascript

Hi,
does javascript support polymorphism (OOPS Concepts). if yes can some body eplain it or give a sample.

looking forward for a earliest reply...

Thanks in advance
Aug 28 '07 #1
5 2421
gits
5,390 Expert Mod 4TB
hi ...

of course ;) ... have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. // constructor of a parent class
  2. function PARENT_CLASS() {
  3. }
  4.  
  5. // a method of the parent class
  6. PARENT_CLASS.prototype.a_method = function() {
  7.     // do something
  8. }
  9.  
  10. // an other method of the parent class
  11. PARENT_CLASS.prototype.b_method = function() {
  12.     // do something
  13. }
  14.  
  15. // constructor of a child class
  16. function CHILD_CLASS_A() {
  17. }
  18.  
  19. // inheriting from parent class
  20. CHILD_CLASS_A.prototype = new PARENT_CLASS;
  21.  
  22. // fixing the constructor - otherwise the instance later on would 
  23. // be of type PARENT_CLASS
  24. CHILD_CLASS_A.prototype.constructor = CHILD_CLASS_A;
  25.  
  26. // override the inherited method now and we have simple polymorphism
  27. CHILD_CLASS_A.prototype.a_method = function() {
  28.     // do something other for the child class here
  29. }
  30.  
  31. // now the usage :)
  32.  
  33. var pcl = new PARENT_CLASS;
  34. pcl.a_method(); // initial behaviour
  35. pcl.b_mehtod(); // initial behaviour
  36.  
  37. var ccl = new CHILD_CLASS_A;
  38. ccl.a_method(); // overridden behaviour
  39. ccl.b_method(); // initial behaviour
  40.  
kind regards
Aug 28 '07 #2
mrhoo
428 256MB
gits, I believe your line 20-
CHILD_CLASS_A.prototype = PARENT_CLASS
should be
CHILD_CLASS_A.prototype =new PARENT_CLASS;
to prevent the child class from overwriting the parent's prototype with its methods.
Aug 28 '07 #3
gits
5,390 Expert Mod 4TB
gits, I believe your line 20-
CHILD_CLASS_A.prototype = PARENT_CLASS
should be
CHILD_CLASS_A.prototype =new PARENT_CLASS;
to prevent the child class from overwriting the parent's prototype with its methods.
thanks ... of course it is a typo ... i'll edit the code ...

kind regards
Aug 28 '07 #4
thanks ... of course it is a typo ... i'll edit the code ...

kind regards
tnx..
I have a doubt though.. is function overloading supported....
also i feel polymorphism (overridding) is supported only thru inheritance can two functions of the same name be found inside a base class..

Looking forward for your comments....

Thanks in advance for your reply.
Sep 3 '07 #5
pbmods
5,821 Expert 4TB
Heya, New Learner.

Just like you can do this:
Expand|Select|Wrap|Line Numbers
  1. var $obj1 ={a: 1, b: 2};
  2. var $obj2 = new Object($obj1);
  3. $obj2.a = 5;
  4.  
  5. alert(print_r($obj1));  // alerts 'Array ( [a] => (Number) 1, [b] => (Number) 2 )'
  6. alert(print_r($obj2));  // alerts 'Array ( [a] => (Number) 5, [b] => (Number) 2 )'
  7.  
to overload a data member of an object, so can you overload a function.

Remember: functions are objects themselves in JavaScript! For more information, check out this article.

print_r() is available here.
Sep 3 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: rashkatsa | last post by:
Hi all ! I have written a little module called 'polymorph' that allows to call different methods of a class that have the same name but that have not the same number of arguments. *args are...
37
by: Mike Meng | last post by:
hi all, I'm a newbie Python programmer with a C++ brain inside. I have a lightweight framework in which I design a base class and expect user to extend. In other part of the framework, I heavily...
3
by: Marshal | last post by:
This is a definite and reproduceable bug in Web Services, Visual Studio 2003. Here is a simple example. Create a web service... // this represents an original version of an object class...
3
by: Richard Tappenden | last post by:
Hi! I'm struggling a bit with Polymorphism in vb.net. There is something I know I can do in C++, but I cannot seem to do it in vb.net (or c# for that matter). Basically, I have an abstract...
2
by: sarathy | last post by:
Hi all, I need a small clarification reg. Templates and Polymorphism. I believe templates is really a good feature, which can be used to implement generic functions and classes. But i doubt...
13
by: Jack | last post by:
I have a class called "Base". This class has a protected member variable "m_base" which can be retrieved using the public member function "GetBaseMember". "m_base" is initialized to "1" and is...
11
by: chsalvia | last post by:
I've been programming in C++ for a little over 2 years, and I still find myself wondering when I should use polymorphism. Some people claim that polymorphism is such an integral part of C++,...
1
weaknessforcats
by: weaknessforcats | last post by:
Introduction Polymorphism is the official term for Object-Oriented Programming (OOP). Polymorphism is implemented in C++ by virtual functions. This article uses a simple example hierarchy which...
17
by: Bart Friederichs | last post by:
Hello, I created the following inheritance: class Parent { public: void foo(int i); }; class Child : public Parent {
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.