473,734 Members | 2,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a method to make js have the ability to inherit

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"+s elf.name);
}
}

a = new Father();
a.hello();
b = new Child();
b.hello();

Nov 20 '06 #1
19 2440
zz***********@y ahoo.com.cn wrote:
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"+s elf.name);
}
}

a = new Father();
a.hello();
b = new Child();
b.hello();
But have you achieved anything more than can be done with normal
javascript inheritance, using a fraction of the code and with
considerably less added complexity? For example, as:-

function Father(){
}
Father.prototyp e = {
hello:function( ){
alert(this.type +this.name);
};
type:"father";
name:"father"
};
function Child(){
}
Child.prototype = new Father():
Child.prototype .type = 'child';
a = new Father();
a.hello();
b = new Child();
b.hello();

Richard.
Nov 20 '06 #2
Richard Cornford wrote:
<snip>
function Father(){
}
Father.prototyp e = {
hello:function( ){
alert(this.type +this.name);
};
type:"father";
name:"father"
};
<snip>

The name value pairs in the object literal should be comma separated not
semicolon separated. I.E:-

Father.prototyp e = {
hello:function( ){
alert(this.type +this.name);
},
type:"father",
name:"father"
};

Richard.
Nov 20 '06 #3
In article <11************ **********@j44g 2000cwa.googleg roups.com>,
zz***********@y ahoo.com.cn writes
>Hi,I find a way to make javescript more like c++ or pyhon
Why ?

John
--
John Harris
Nov 20 '06 #4
Richard Cornford escreveu:
Richard Cornford wrote:
The name value pairs in the object literal should be comma separated not
semicolon separated. I.E:-
On the example there's another small mistake: "new Father():"
Father.prototyp e = {
hello:function( ){
alert(this.type +this.name);
},
type:"father",
name:"father"
};
This looks cute, but it damages the language, because this should work:

var a = new Father, b = new a.constructor;

//#1
alert((a instanceof Father) + "\n" + (b instanceof Father));

So to correct it:

Father.prototyp e = {
hello:function( ){
alert(this.type +this.name);
},
type:"father",
name:"father",
constructor: Father
};

or

new function(){
var o = Father.prototyp e;
o.type = "father";
:
:
};
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 21 '06 #5
Jonas Raoni wrote:
Richard Cornford escreveu:
>Richard Cornford wrote:
The name value pairs in the object literal should be comma
separated not semicolon separated. I.E:-

On the example there's another small mistake: "new Father():"
You will have the explain that as I don't see it.
>Father.prototy pe = {
hello:function( ){
alert(this.type +this.name);
},
type:"father",
name:"father"
};

This looks cute, but it damages the language, because this
should work:

var a = new Father, b = new a.constructor;
Why "should" it work? It will work if no object is assigned to a
constructor's prototype but that is only a feature of the language, it
is not actually useful.
//#1
alert((a instanceof Father) + "\n" + (b instanceof Father));

So to correct it:

Father.prototyp e = {
hello:function( ){
alert(this.type +this.name);
},
type:"father",
name:"father",
constructor: Father
};
Why include an assignment to a - constructor - property if nobody is
going to use it?
new function(){
var o = Father.prototyp e;
o.type = "father";
:
:
};
And I don't see any point in doing that at all.

Richard.
Nov 21 '06 #6
Richard Cornford escreveu:
Jonas Raoni wrote:
>On the example there's another small mistake: "new Father():"

You will have the explain that as I don't see it.
":" instead of ";" :)
>This looks cute, but it damages the language, because this
should work:

var a = new Father, b = new a.constructor;

Why "should" it work? It will work if no object is assigned to a
constructor's prototype but that is only a feature of the language, it
is not actually useful.
Don't worry, I just want to be boring... But that's true, till now I
just used the constructor once or twice.
>Father.prototy pe = {
constructor: Father
};

Why include an assignment to a - constructor - property if nobody is
going to use it?
I suppose you've written this way to avoid writing several assignments
to the "Father.prototy pe". But if you can keep the language features
intact, why not?

It's not something dangerous as can be prototyping the Array/Object, but
*I* preffer to keep the features intact.
>new function(){
var o = Father.prototyp e;
o.type = "father";
};

And I don't see any point in doing that at all.
Hmm, it's just a closure with a shortcut variable, something like this:

with({o: a.prototype}){
o.abc = 123;
}

Just other ways to avoid overwriting the constructor.
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 22 '06 #7
Jonas Raoni escreveu:
Richard Cornford escreveu:
>Jonas Raoni wrote:
Don't worry, I just want to be boring... But that's true, till now I
just used the constructor once or twice.
Just to mention, I remembered one of the places where I used the
constructor:

alert("" instanceof String);
//i could use typeof, but in my case this looked better because "String"
was an argument
alert("".constr uctor == String);
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 22 '06 #8
Jonas Raoni wrote:
Richard Cornford escreveu:
>Jonas Raoni wrote:
>>On the example there's another small mistake: "new Father():"

You will have the explain that as I don't see it.

":" instead of ";" :)
Ah, I see. Thank you. Another typo corrected.
>>This looks cute, but it damages the language, because
this should work:

var a = new Father, b = new a.constructor;

Why "should" it work? It will work if no object is assigned to
a constructor's prototype but that is only a feature of the
language, it is not actually useful.

Don't worry, I just want to be boring... But that's true, till
now I just used the constructor once or twice.
I have never seen a reason for using the -constructor - property of any
object, and where I have seen others use it the resulting code ended up
in a style that I would consider inappropriate to javascript.
>>Father.protot ype = {
constructor: Father
};

Why include an assignment to a - constructor - property
if nobody is going to use it?

I suppose you've written this way to avoid writing several
assignments to the "Father.prototy pe". But if you can keep
the language features intact, why not?
If there is no reason for using the property any effort to "keep it
intact" is futile and wasteful.
It's not something dangerous as can be prototyping the
Array/Object, but *I* preffer to keep the features intact.
I am generally expected to write efficient code, so I don't have ti doe
what is not needed or useful.
>>new function(){
var o = Father.prototyp e;
o.type = "father";
};

And I don't see any point in doing that at all.

Hmm, it's just a closure with a shortcut variable,
There is no closure there. And the result of the expression is an object
that is not an instance of - Father -, does not have - Father.protoype -
on its prototype chain and does not have - Father - as its -
constructor -, so what is the point?
something like
this:

with({o: a.prototype}){
o.abc = 123;
}
Nor there.
Just other ways to avoid overwriting the constructor.
If that was a useful thing to be doing then your earlier methods make
more sense.

Richard.
Nov 26 '06 #9
Richard Cornford escreveu:
I have never seen a reason for using the -constructor - property of any
object, and where I have seen others use it the resulting code ended up
in a style that I would consider inappropriate to javascript.
Hmmm, I just remember of using it in a situation similar to this one:

"" instanceof String
"".construc tor == String

I could use typeof (which also looks better, since it's a native type),
but I preferred comparing the constructor with the given argument.
If there is no reason for using the property any effort to "keep it
intact" is futile and wasteful.
I don't think so, I don't like to assume anything as world truth and,
"fixing" it isn't what I can call a hard job :]
>It's not something dangerous as can be prototyping the
Array/Object, but *I* preffer to keep the features intact.

I am generally expected to write efficient code, so I don't have ti doe
what is not needed or useful.
It's ok, everybody has its own point of view.
>>>new function(){
var o = Father.prototyp e;
o.type = "father";
};
And I don't see any point in doing that at all.
Hmm, it's just a closure with a shortcut variable,

There is no closure there. And the result of the expression is an object
that is not an instance of - Father -, does not have - Father.protoype -
on its prototype chain and does not have - Father - as its -
constructor -, so what is the point?
Didn't you perceive the code purpose???

Some people like to do it this way:

var o = Father.prototyp e;
o.type = "father";
o.name = "lala";

Instead of writing:

Father.prototyp e.type = "father";
Father.prototyp e.name = "lala";

So, "o" is just a shortcut for the prototype, what I've done was just to
enclose the variable inside the function to avoid collisions. And used
"new function(){}" instead of "(function(){}) ()".

>something like
this:

with({o: a.prototype}){
o.abc = 123;
}

Nor there.
I won't explain the purpose again, it's the same.

var o = 0;
with({o: 1})
o = 2;
alert(o);

>Just other ways to avoid overwriting the constructor.

If that was a useful thing to be doing then your earlier methods make
more sense.
Ah, there are a lot of ways to do the same thing, fixing the constructor
or not, it's up to you ^^
--
Jonas Raoni Soares Silva
http://www.jsfromhell.com
Nov 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
1876
by: jlopes | last post by:
I'm looking at the differences between these to forms and see no difference in their use. When accessed through a derived class. class ABase { public: virtual void filter(){ /* some code */ } }; class D_of_ABase : public ABase
1
1262
by: Mark | last post by:
I have class1 and class2. When I inherit from class1 or class2, I want to have to implement a method called MyMethod(). If this was my only requirement, I'd make MyMethod and abstract method in both class1 and class2. However, class2 also needs to inherit from class1. Hence, class2 has to have some implementation of MyMethod(). I believe I won't be able to make it abstract in class2 since it's supposed to implement all abstract...
6
3013
by: Alex Sedow | last post by:
Example 1 interface I { string ToString(); } public class C : I { public void f() {
3
1264
by: A.Bekiaris | last post by:
Hi I have an abstract class with some virtual methods which I inherit and create say Class1 I want to give end users the ability to override the virtual methods of the base class How this can be implemented? thnks
7
2233
by: Byron | last post by:
I have several user controls that have a few methods in common, such LoadFromForm() which populates an object from controls on the form. I want to call that method from the form in which the control is contained regardless of the type currently displayed without having to use a huge switch somthing like: switch(curentUserType.GetType()) { case "Person": ((person)currentUserControl).LoadFromForm();
15
12788
by: jon | last post by:
How can I call a base interface method? class ThirdPartyClass :IDisposable { //I can not modify this class void IDisposable.Dispose() { Console.WriteLine( "ThirdPartyClass Dispose" ); } } class MyClass :ThirdPartyClass, IDisposable { void IDisposable.Dispose() {
3
1654
by: comp.lang.php | last post by:
class ThumbView extends PaginationView { function ThumbView() {} /** * Determine if the original image is actually an image and thus a thumbnail can be generated at all * * @access public * @param mixed $image_name (optional)
2
2379
by: Joe | last post by:
Hi I have a typed dataset and I want to add a method to modify a value in the datarow. I want to inherit from the class so if the XSDchanges I would lose my code when the class regenerates This is what I am trying with no luck is below. I cannot see JobID in intellisense after I inherit. I assume due to it being private
6
27812
by: bryanbabula | last post by:
I have a question about overriding i was wondering if anyone could help me with, or even suggesting a better/different way. I have no idea if this can even be done or not. I was wondering if there was anyway to force a class to call a base class's method that it is overriding? Almost the same way you have to call a base class's constructor if it has arguments. (example ** assuming the Person class's constructor has (string FirstName) as...
0
8776
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9449
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9236
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9182
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2724
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.