ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
so it seems from the looks of things that js is so dynamic right (at least at the moment) that its authors at EMCA have left the inheritance issue open. (to both the advantage and confusion or even dismay of developers)
okay , enough of the small talk
does anybody know how inheritance goes in js?
here is some code that you may take as a benchmark (THIS CODE IS IMPROVISED)
Code:
function point(){ // class point
this.set_position = function(x,y){
this.pos_x = x;
this.pos_y = y;
}
this.get_position = function(){
pos = new Array;
pos[x] = this.pos_x;
pox[y] = this.pos_y;
return pos;
}
}
function circle(){ // class circle
// ATTENTION WE NEED inheritance from class point here !!!
this.set_radias = function(r){
this.radius = r;
}
this.get_circumference = function(){
return this.radius * PI * 2;
}
}
// Okay , it's Instantiation Time !!
var omega = new circle();
omega.set_position(34,-1);
omega.set_radius(5);
alert("Omega Coordinates: " + omega.get_position());
alert("Omega Circumference: " + omega.get_circumference());
//you get the deal ....
No, the question is not obvious - it is not clear why inheritance from 'point' is needed in the first place
well ,i hate to be rude but that's a common example used to demonstrate how object oriented programming works , DOH !
for starters the function set_position and get_position used in point are eventually gonna be the same ones used for circle since every circle has a point (it's called the CENTER of circle)
Quote:
And are functions first class objects in JavaScript ?
I'm afraid , now you've to explain to me what you're trying to ask..
all i can say is that , what you've seen is the common syntax used to declare js classes and their methods and variables.
btw , it looks from the signature of yours that you're more knowledgable in python than in js , you don't have to solve this problem if this is outside of realm of expertize...
...
i need a technique in js that automatically does this operation without having to "inherit" every single function manually
in other words i need syntactic sugar !
cheers
Back to my question about the need of inheritance - if I understrand correctly, in order to access set position methods in your 'circle' instance you need two table lookups - first in 'circle' instance and then in it's 'point' instance - unless JavaScript compiler is optimizing.
Regarding the need of syntactic sugar - write or use a text preprocessor.
Back to my question about the need of inheritance - if I understrand correctly, in order to access set position methods in your 'circle' instance you need two table lookups - first in 'circle' instance and then in it's 'point' instance - unless JavaScript compiler is optimizing.
Regarding the need of syntactic sugar - write or use a text preprocessor.
table look ups ? preprocessor ? what are you talking about ?
Javascript is an INTERPRETED language there is no pre-processing !
the issue is HOW to do inheritance in js not to explain why , that's beyond the scope this thread..
This is you who are writing the code and this is you who chooses what tools to use. You can use a preprocessor.
In fact, I have my own preprocessor written in Perl and sometimes I use it to produce Perl code.
I KNOW what you mean !
you're suggesting to use a server side language such as php , perl ..etc to write the necessary javascript in realtime before being submitted to the browser....
well what you've described is what we web developers refer to as a "browser hack" (remember this term)
and it's NOT an option !
because :
1) it shifts the load back on the server which is what client side scripting languages such as js are supposed to solve.
2) it complicates things and makes debugging js code a mess
Quote:
How do you call function in an interpreted language ?
the same way i've done it in the previous code section...
you're suggesting to use a server side language such as php , perl ..etc to write the necessary javascript in realtime before being submitted to the browser....
well what you've described is what we web developers refer to as a "browser hack" (remember this term)
and it's NOT an option !
because :
1) it shifts the load back on the server which is what client side scripting languages such as js are supposed to solve.
2) it complicates things and makes debugging js code a mess
the same way i've done it in the previous code section...
cheers
No, I didn't mean that. You write JavaScript once on your machine, and during that session you use whatever tools you like. So no additional load on server whatsoever.
And
Quote:
the same way i've done it in the previous code section.
is not an answer, i.e. it doesn't describe the mechanism.
The guy is not going to solve your "problem" for you, not at least directly, if not at all. You might want to post your question to stackoverflow.com. There are more experts there on Javascript who can answer your question.
Last edited by ghostdog74; 09-02-2010 at 09:38 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.