Return value of a code block

A

aundro

Hello all,

I was wondering whether I could do something like:

--snip--

MyType.prototype.myProp = {var xxx = 'this is a string'; xxx.substring(4);}

--snip--

and use the return value of the last computed expression as the one to
assign to the 'myProp' property.
Of course, I could put that code in a separate function and then call
it.. this is not a stopper, but I wondered if I could do it.


Thank you for any help/advice!

Arnaud


--
Arnaud DIEDEREN
Software Developer
IONIC Software
Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
Tel: +32.4.3640364 - Fax: +32.4.2534737
mailto:[email protected]
http://www.ionicsoft.com
 
E

Evertjan.

aundro wrote on 24 okt 2005 in comp.lang.javascript:
I was wondering whether I could do something like:

--snip--

MyType.prototype.myProp = {var xxx = 'this is a string';
xxx.substring(4);}

--snip--

and use the return value of the last computed expression as the one to
assign to the 'myProp' property.
Of course, I could put that code in a separate function and then call
it.. this is not a stopper, but I wondered if I could do it.

The proof of the pudding is in the eating. ;-)

No, you cannot execute a prototype as a string,
it has to be a function, methinks.
 
J

Julian Turner

aundro said:
Hello all,

I was wondering whether I could do something like:

--snip--

MyType.prototype.myProp = {var xxx = 'this is a string'; xxx.substring(4);}

--snip--

and use the return value of the last computed expression as the one to
assign to the 'myProp' property.
Of course, I could put that code in a separate function and then call
it.. this is not a stopper, but I wondered if I could do it.


Thank you for any help/advice!

Arnaud

Yes, I think you can. But you cannot use "var", so you must define the
variable name separately, or not use a variable name. The key is to
use the "," token, and "()"'s.

var xxx;
function Test()
{

}
Test.prototype.myProp=(xxx = 'this is a string', xxx.substring(0,4));
Test.prototype.myProp2=(('this is a string').substring(0,4));

var t=new Test();
alert(t.myProp);
alert(t.myProp2);


Julian
 
R

Random

aundro said:
Hello all,

I was wondering whether I could do something like:

--snip--

MyType.prototype.myProp = {var xxx = 'this is a string'; xxx.substring(4);}

--snip--

and use the return value of the last computed expression as the one to
assign to the 'myProp' property.
Of course, I could put that code in a separate function and then call
it.. this is not a stopper, but I wondered if I could do it.


There're a couple of things that it looks like you could be after, and
I'm not sure which it is:
1. Use a code block's return value without calling it as a function,
purely for the sake of concise code and 'cool' logic

2. Assign a code block as the property, so that either:

2a. when the object is instantiated, the code is executed and its
return value assigned to the property

2b. each time the value of the property is retrieved, the code is
executed and its return value is given as the value of the property

To all of which the simple answer is: no, you can't. There're more
complicated browser-specific answers that I've seen and don't recall,
but they aren't useful in a public-www context.

The alternative to all of the above is, as you said, to just use a
function.
 
J

Julian Turner

Julian said:
Yes, I think you can.

Sorry, to be more precise. I don't think you can use a "Block
Statement", because in the grammar context you are referring to,
Object.prototype.Identifier={}, the Javascript parser is looking to
make an Object Literal, not a Block Statment.

The alternative is to use ()'s, these can contain and return the result
of another Expression.

It would be useful to understand why you want to do this in the first
place, as there may be a better way for you to achieve your desired
result.

Julian
 
D

Douglas Crockford

I was wondering whether I could do something like:
--snip--

MyType.prototype.myProp = {var xxx = 'this is a string'; xxx.substring(4);}

--snip--

and use the return value of the last computed expression as the one to
assign to the 'myProp' property.
Of course, I could put that code in a separate function and then call
it.. this is not a stopper, but I wondered if I could do it.

MyType.prototype.myProp = function () {var xxx = 'this is a string';
xxx.substring(4);}();

http://www.crockford.com/javascript
 
A

aundro

Julian Turner said:
Julian Turner wrote:

It would be useful to understand why you want to do this in the first
place, as there may be a better way for you to achieve your desired
result.

Hi,

what I wanted to do is initialize a type field (Analogy: a static
field in a Java class) with a value that requires some treatment.
Something like:

---------snip---------

MyType.prototype.myField = {
var retVal = document.createElement ("div");
retVal.style.top = "10px";
retVal.style.left = "10px";
return retVal;
}

---------snip---------

That's it :)
I just wanted to avoid defining a toplevel function to do the treatment.

Thank you all very much for your advices and replies

Arnaud


--
Arnaud DIEDEREN
Software Developer
IONIC Software
Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
Tel: +32.4.3640364 - Fax: +32.4.2534737
mailto:[email protected]
http://www.ionicsoft.com
 
J

Julian Turner

aundro said:
what I wanted to do is initialize a type field (Analogy: a static
field in a Java class) with a value that requires some treatment.
Something like:

---------snip---------

MyType.prototype.myField = {
var retVal = document.createElement ("div");
retVal.style.top = "10px";
retVal.style.left = "10px";
return retVal;
}

---------snip---------

That's it :)
I just wanted to avoid defining a toplevel function to do the treatment.


Hi. The simplest solution is probably to adopt Douglas Crockford's
suggestion of using Function Expressions. Namely:-

MyType.prototype.myField = (function(){
var retVal = document.createElement ("div");
retVal.style.top = "10px";
retVal.style.left = "10px";
return retVal;
})();

This does involve creating a function, but you do not need to declare
it with a name. It is known as a Function Expression.

How Douglas's solution works is:-

1. Javascript permits Primary Expressions in the form "(Expression)".
Effectively the "(...)" returns the value of the expression.

2. In this case "(function(){})" is returning a function created
using a Function Expression.

3. By adding "()" at the end giving "(function(){})()" this
effectively calls the newly created function, and retVal becomes the
value for myField.

If you don't want a function at all, then my solution is perhaps the
next option, but it is a little inelegant.

Julian
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top