make js like c++ or python

Z

zzw8206262001

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();
 
J

Jonas Raoni

(e-mail address removed) 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();
 
Z

zzw8206262001

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,113
Latest member
Vinay KumarNevatia
Top