By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,466 Members | 1,148 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,466 IT Pros & Developers. It's quick & easy.

make js like c++ or python

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.