Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Named arguments and inheritance

Reply
Thread Tools

Named arguments and inheritance

 
 
VK
Guest
Posts: n/a
 
      11-14-2006

Pacific Fox wrote:
> I've got a base object that works fine with named arguments when called
> on it's own. However when I call the child object I get an error
> "[associative array name] has no properties (in firefox)."
>
> I simple test:
>
> Property = function( arg ) {
> /**
> * Private properties
> */
> var name = arg.name; // required, the name of the field
>
> /**
> * Priviliged methods, may be invoked publicly and may access private
> variables
> */
> this.getName = function() {
> return name;
> }
> };
>
> /**
> * Class for form field properties, extends property
> */
> FormProperty = function( arg ) {
> /**
> * Inheritance
> */
> this.super = Property;
> this.super( { name:arg.name } );
> this.prototype = new Property;
> }
> //property = new FormProperty( { name:"myVar" } )
> property = new Property( { name:"test" } )
> alert( property.getName() );
>
> Un comment //property = new FormProperty( { name:"myVar" } )
> to get the error.
>
> Thanks in advance for any help.


You problem is that you mixing two types of constructors: object
constructor (the most regular one) and constructor constructor
(factory). In your code Property is a constructor factory returning new
constructor instance - while you are trying to use it as a regular
object constructor.

<html>
<head>
<title>Checkboxes</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">

<script type="text/javascript">

function Property() {
return (
function(arg) {
var name = arg.name;
this.getName = function() {
return name;
}
});
}

function FormProperty(arg) {
this.privProperty = new Property;
this.privProperty( { name:arg.name } );
}

property = new FormProperty( { name:"myVar" } )
alert( property.getName() );
</script>

</head>

<body>
</body>
</html>

 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      11-14-2006
VK wrote:
> Pacific Fox wrote:
>> I've got a base object that works fine with named arguments when called
>> on it's own. However when I call the child object I get an error
>> "[associative array name] has no properties (in firefox)."
>>
>> I simple test:
>>
>> Property = function( arg ) {
>> /**
>> * Private properties
>> */
>> var name = arg.name; // required, the name of the field
>>
>> /**
>> * Priviliged methods, may be invoked publicly and may access
>> private variables
>> */
>> this.getName = function() {
>> return name;
>> }
>> };
>>
>> /**
>> * Class for form field properties, extends property
>> */
>> FormProperty = function( arg ) {
>> /**
>> * Inheritance
>> */
>> this.super = Property;
>> this.super( { name:arg.name } );
>> this.prototype = new Property;
>> }
>> //property = new FormProperty( { name:"myVar" } )
>> property = new Property( { name:"test" } )
>> alert( property.getName() );
>>
>> Un comment //property = new FormProperty( { name:"myVar" } )
>> to get the error.
>>
>> Thanks in advance for any help.

>
> You problem is that you mixing two types of constructors: object
> constructor (the most regular one) and constructor constructor
> (factory). In your code Property is a constructor factory returning new
> constructor instance - while you are trying to use it as a regular
> object constructor.


You really do talk a lot of nonsense.

<snip>
> function Property() {
> return (
> function(arg) {
> var name = arg.name;
> this.getName = function() {
> return name;
> }
> });
> }
>
> function FormProperty(arg) {
> this.privProperty = new Property;
> this.privProperty( { name:arg.name } );
> }

<snip>

The issue of 'subclassing' is inherent in the question; this rubbish is
not only insanely designed but has also removed the Property 'class'
(and any notion of 'subclassing') entirely.

Richard.

 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      11-14-2006
> > You problem is that you mixing two types of constructors: object
> > constructor (the most regular one) and constructor constructor
> > (factory). In your code Property is a constructor factory returning new
> > constructor instance - while you are trying to use it as a regular
> > object constructor.


> You really do talk a lot of nonsense.


About the difference between
1) constructor returning new object instances of some kind
2) constructor returning constructor to produce new object instances of
some kind
?

That is not a "nonsense" but an important distinction.
"A returns new instances of O; B returns new instance of A able to
return new instances of O".
In your opinion A and B do the same job?


> <snip>
> > function Property() {
> > return (
> > function(arg) {
> > var name = arg.name;
> > this.getName = function() {
> > return name;
> > }
> > });
> > }
> >
> > function FormProperty(arg) {
> > this.privProperty = new Property;
> > this.privProperty( { name:arg.name } );
> > }

> <snip>


> The issue of 'subclassing' is inherent in the question; this rubbish is
> not only insanely designed but has also removed the Property 'class'
> (and any notion of 'subclassing') entirely.


It seems that you did not understand what does the code do.

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      11-14-2006
VK wrote:
>>> You problem is that you mixing two types of constructors: object
>>> constructor (the most regular one) and constructor constructor
>>> (factory). In your code Property is a constructor factory returning new
>>> constructor instance - while you are trying to use it as a regular
>>> object constructor.

>
> You really do talk a lot of nonsense.
>
> About the difference between
> 1) constructor returning new object instances of some kind
> 2) constructor returning constructor to produce new object instances of
> some kind
> ?


There should be no "constructor" returning a constructor. If a function
is to return a function (even if it is to be used as a constructor
(which yours is not)) then it should not be called using the - new -
operator, and should not be thought of, or spoken of, as a constructor.

> That is not a "nonsense" but an important distinction.


It is nonsense as soon as you start talking of "constructor returning
constructor", and such talk may lead people to the even greater folly
of applying the - new - operator to functions that are not being used
to create object instances.

> "A returns new instances of O; B returns new instance of A able to
> return new instances of O".
> In your opinion A and B do the same job?


They are not both constructors.

>> <snip>
>>> function Property() {
>>> return (
>>> function(arg) {
>>> var name = arg.name;
>>> this.getName = function() {
>>> return name;
>>> }
>>> });
>>> }
>>>
>>> function FormProperty(arg) {
>>> this.privProperty = new Property;
>>> this.privProperty( { name:arg.name } );
>>> }

>> <snip>

>
>> The issue of 'subclassing' is inherent in the question; this rubbish is
>> not only insanely designed but has also removed the Property 'class'
>> (and any notion of 'subclassing') entirely.

>
> It seems that you did not understand what does the code do.


Does it? Why do you think that then? Are you saying that your code does
contain something that represents a means of creating Property
instances? are you saying there is some reason for using the - new -
operator with your - Property - function? Or that there is some point
in having a reference to a function as - privProperty?

If all is wanted is an ability to augment an object with a closure to
store a name and a method for retreating it wouldn't:-

function addNameInterface(name, obj){
obj.getName = function(){
return name;
};
}
function FormProperty(arg) {
addNameInterface(arg.name, this);
}

- get there quicker, cleaner, more directly and in a lot less code?

Richard.

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      11-14-2006
> There should be no "constructor" returning a constructor. If a function
> is to return a function (even if it is to be used as a constructor
> (which yours is not)) then it should not be called using the - new -
> operator, and should not be thought of, or spoken of, as a constructor.


Really? Says who? JavaScript function can be used as a simple function
or as a constructor. That is it exactly can be defined at the moment
of the call *only*. The very same function can be used in both ways. If
it's called with "new" keyword then it acts as constructor, otherwise
as a simple function. By just adding
this.property = something;
into function body you don't tranform it in some magic way into
constructor.

> > "A returns new instances of O; B returns new instance of A able to
> > return new instances of O".
> > In your opinion A and B do the same job?


> They are not both constructors.


They are: but they are constructing different things.

> If all is wanted is an ability to augment an object with a closure to
> store a name and a method for retreating it wouldn't:-
>
> function addNameInterface(name, obj){
> obj.getName = function(){
> return name;
> };
> }
> function FormProperty(arg) {
> addNameInterface(arg.name, this);
> }
>
> - get there quicker, cleaner, more directly and in a lot less code?


Mostly because it's not what OP was after If I'm mistaken OP will
correct me.

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      11-14-2006
VK wrote:
>> There should be no "constructor" returning a constructor. If a function
>> is to return a function (even if it is to be used as a constructor
>> (which yours is not)) then it should not be called using the - new -
>> operator, and should not be thought of, or spoken of, as a constructor.

>
> Really? Says who? JavaScript function can be used as a simple function
> or as a constructor.


But there is no point in calling a function as a constructor (with the
- new - operator) if it returns any object other than the value of the
- this - keyword. Doing so just results in the creation of a
superfluous object and then the throwing away of that object. There is
no need for any other reason for never calling a function that returns
any object other than - this - a constructor, or as a constructor. All
the active effects of doing so would be achieved by just calling it as
a function, and the useless side effects (and associated overheads) are
then avoided, along with the conceptual misdirection following from the
inappropriate use of the - new - operator.

> That is it exactly can be defined at the moment
> of the call *only*.


And in English?

> The very same function can be used in both ways.


Not very effectively, and designing with such an intent would be
perverse.

> If it's called with "new" keyword then it acts as constructor,


An object is created as a result, but if that object is never employed
it becomes very questionable whether the function is acting as a
constructor, or should be called as one.

> otherwise as a simple function. By just adding
> this.property = something;
> into function body you don't tranform it in some magic way into
> constructor.


What is the "it" you are referring to?

>>> "A returns new instances of O; B returns new instance of A able to
>>> return new instances of O".
>>> In your opinion A and B do the same job?

>
>> They are not both constructors.

>
> They are:


No they are not. the latter may be categorised a 'factory' for
constructor instances but it is not a constructor.

> but they are constructing different things.
>
>> If all is wanted is an ability to augment an object with a closure to
>> store a name and a method for retreating it wouldn't:-
>>
>> function addNameInterface(name, obj){
>> obj.getName = function(){
>> return name;
>> };
>> }
>> function FormProperty(arg) {
>> addNameInterface(arg.name, this);
>> }
>>
>> - get there quicker, cleaner, more directly and in a lot less code?

>
> Mostly because it's not what OP was after


I know the OP was not after that, it is a more sensible implementation
of the nonsense code you posted. The OP is interested in subclassing,
for which your code was irrelevant.

> If I'm mistaken OP will
> correct me.


Richard.

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      11-14-2006

Richard Cornford wrote:
> But there is no point in calling a function as a constructor (with the
> - new - operator) if it returns any object other than the value of the
> - this - keyword.


Really? Well, I guess it depends on the chosen coding style. The
following code is polemic (to your statement) so a bit too complicated
to show different options at once:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">

<script type="text/javascript">

function F(arg) {

if (typeof arg != 'string') {
return new Boolean(false);
}

var secret = arg;

this.secret = new Object;

this.secret.getSecret = function() {
window.alert(secret);
}

return this.secret;
}

var f0 = new F(null);
var f1 = new F('foo');
var f2 = new F('bar');

if (f0 != false) {
f0.getSecret();
}

if (f1 != false) {
f1.getSecret();
}

if (f2 != false) {
f2.getSecret();
}
</script>
</head>

<body>
</body>
</html>

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      11-14-2006

VK wrote:
> The
> following code is polemic (to your statement) so a bit too complicated
> to show different options at once:


or even like this:

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;
charset=iso-8859-1">
<script>

function F() {
var $ = this;
var private = 'empty'
;
this.setPrivate = function(arg) {
private = arg || 'empty';
return $;
}

this.getPrivate = function() {
return private;
}
}

var f = (new F).setPrivate('foo');
alert(f.getPrivate());
</script>
</head>

<body>
</body>
</html>

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      11-14-2006
VK wrote:
> Richard Cornford wrote:
> > But there is no point in calling a function as a constructor (with the
> > - new - operator) if it returns any object other than the value of the
> > - this - keyword.

>
> Really? Well, I guess it depends on the chosen coding style.


You can chose to program like a halfwit, if you must.

> The following code is polemic (to your statement) so a bit too
> complicated to show different options at once:

<snip>
> function F(arg) {
>
> if (typeof arg != 'string') {
> return new Boolean(false);
> }
>
> var secret = arg;
>
> this.secret = new Object;
>
> this.secret.getSecret = function() {
> window.alert(secret);
> }
>
> return this.secret;
> }
>
> var f0 = new F(null);
> var f1 = new F('foo');
> var f2 = new F('bar');

<snip>

And once again you post code without stating what it is supposed to
demonstrate. Presumably the intention was to show that there is some
purpose in treating a function as a constructor when it does not return
the object constructed. If so it fails to achieve that as substituting
the above with:-

function f(arg){
if(typeof arg != 'string'){
return new Boolean(false);
}else{
return ({getSecret:function(){alert(arg);}});
}
}

var f0 = f(null);
var f1 = f('foo');
var f2 = f('bar');

- achieves exactly the same outcome without treating a function that is
not acting as a constructor as if it was a constructor (and again does
so with considerably less code, and less convoluted code.

There is no benefit to writing inefficient, indirect and confusing
code, and being able to is hardly an excluse.

Richard.

 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      11-14-2006
VK wrote:
> VK wrote:
>> The
>> following code is polemic (to your statement) so a bit too
>> complicated to show different options at once:

>
> or even like this:


You blithering idiot: var f = (new F).setPrivate('foo'); _is_ calling a
function that returns the - this - object as a constructor. It is not
relevant to the question of whether functions that do not return the -
this - object should be subject to the - new - operator.

<snip>
> function F() {
> var $ = this;
> var private = 'empty'
> ;
> this.setPrivate = function(arg) {
> private = arg || 'empty';
> return $;
> }
>
> this.getPrivate = function() {
> return private;
> }
> }


Why the needless complexity?

function F() {
var private = 'empty' ;
this.setPrivate = function(arg) {
private = arg || private;
return this;
};
this.getPrivate = function() {
return private;
};
}

- Does the same job.

> var f = (new F).setPrivate('foo');


What is the advantage of this indirect construct over the more normal:-

var f = new F;
f.setPrivate('foo');

-or:-

var f;
(f = new F).setPrivate('foo)';

- and not having to return the object instance from the - setPrivate -
method (which should probably be returning an indicator of its success,
if anything at all).

Richard.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
named parameter idiom and inheritance Christof Warlich C++ 8 06-17-2012 08:51 AM
[ANN] rdp-arguments -- named arguments for 1.8 and 1.9! Roger Pack Ruby 2 01-04-2010 02:56 AM
equivalent of named arguments for constructors Andrey Vul C++ 5 11-04-2009 06:43 AM
Passing variable number of named arguments Ramashish Baranwal Python 2 12-28-2006 05:36 AM
functions and named keyword arguments Fuzzyman Python 2 02-22-2005 08:00 AM



Advertisments