469,923 Members | 1,543 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,923 developers. It's quick & easy.

make js like c++ or python

Hi,I find a way to make javescript more like c++ or pyhon
There is the sample code:

function Father(self) //every contructor
//may have "self" argument
{
self=self?self:this; //every class may
//have this statement
self.hello = function()
{
alert("father"+self.name);
}
self.name = "baibai";
}
function Child(self) //every contructor
//may have "self" argument
{
self=self?self:this; //every class may
//have this statement

//inherit from faher
Father(self);
self.hello = function()
{
alert("child"+self.name);
}
}
a = new Father();
a.hello();
b = new Child();
b.hello();

Nov 20 '06 #1
2 1096
zz***********@yahoo.com.cn escreveu:
Hi,I find a way to make javescript more like c++ or pyhon
JavaScript isn't C++ nor Python, it's a prototyped language.
There is the sample code:
[...]
The "right" way of doing what you want is:

function Parent(){
}
Parent.prototype.test = function(){
alert("It works.");
};

function Child(){
}
Child.prototype = new Parent;

(new Child).test();
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 21 '06 #2
Good, Thanks!
I am used to develop in c/c++.I studied javascript a month ago.I found
it surprising why
javascript not has a obvious concept of class like c++,or provides a
method to inherit.
you are method is better,haha,thanks!

Nov 26 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Berthold Hoellmann | last post: by
1 post views Thread by Tim Bradshaw | last post: by
reply views Thread by Christopher De Vries | last post: by
14 posts views Thread by BOOGIEMAN | last post: by
28 posts views Thread by Steven Bethard | last post: by
7 posts views Thread by Steven Bethard | last post: by
reply views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.