Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   a method to make js have the ability to inherit (http://www.velocityreviews.com/forums/t928271-a-method-to-make-js-have-the-ability-to-inherit.html)

zzw8206262001@yahoo.com.cn 11-20-2006 09:15 AM

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

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


Richard Cornford 11-20-2006 09:52 AM

Re: a method to make js have the ability to inherit
 
zzw8206262001@yahoo.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"+self.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.prototype = {
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.



Richard Cornford 11-20-2006 10:37 AM

Re: a method to make js have the ability to inherit
 
Richard Cornford wrote:
<snip>
> function Father(){
> }
> Father.prototype = {
> 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.prototype = {
hello:function(){
alert(this.type+this.name);
},
type:"father",
name:"father"
};

Richard.



John G Harris 11-20-2006 08:34 PM

Re: a method to make js have the ability to inherit
 
In article <1164014145.485134.234830@j44g2000cwa.googlegroups .com>,
zzw8206262001@yahoo.com.cn writes
>Hi,I find a way to make javescript more like c++ or pyhon


Why ?

John
--
John Harris

Jonas Raoni 11-21-2006 05:11 AM

Re: a method to make js have the ability to inherit
 
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.prototype = {
> 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.prototype = {
hello:function(){
alert(this.type+this.name);
},
type:"father",
name:"father",
constructor: Father
};

or

new function(){
var o = Father.prototype;
o.type = "father";
:
:
};


--
Jonas Raoni Soares Silva
http://www.jsfromhell.com

Richard Cornford 11-21-2006 08:15 AM

Re: a method to make js have the ability to inherit
 
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.prototype = {
>> 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.prototype = {
> 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.prototype;
> o.type = "father";
>:
>:
> };


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

Richard.



Jonas Raoni 11-22-2006 03:46 AM

Re: a method to make js have the ability to inherit
 
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.prototype = {
>> 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.prototype". 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.prototype;
>> 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

Jonas Raoni 11-22-2006 04:32 AM

Re: a method to make js have the ability to inherit
 
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("".constructor == String);


--
Jonas Raoni Soares Silva
http://www.jsfromhell.com

Richard Cornford 11-26-2006 04:44 AM

Re: a method to make js have the ability to inherit
 
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.prototype = {
>>> 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.prototype". 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.prototype;
>>> 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.



Jonas Raoni 11-26-2006 08:29 AM

Re: a method to make js have the ability to inherit
 
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
"".constructor == 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.prototype;
>>>> 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.prototype;
o.type = "father";
o.name = "lala";

Instead of writing:

Father.prototype.type = "father";
Father.prototype.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


All times are GMT. The time now is 12:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.