469,600 Members | 2,246 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,600 developers. It's quick & easy.

Return value of a code block


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:ad@ionicsoft.com
http://www.ionicsoft.com
Oct 24 '05 #1
8 1756
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.

--
Evertjan.
The Netherlands.
(Replace all crosses with dots in my emailaddress)

Oct 24 '05 #2

aundro wrote:
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

Oct 24 '05 #3
aundro wrote:
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.

--
-r

Oct 24 '05 #4

Julian Turner wrote:
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

Oct 24 '05 #5
> 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
Oct 24 '05 #6
"Julian Turner" <ju****@baconbutty.com> writes:
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:ad@ionicsoft.com
http://www.ionicsoft.com
Oct 24 '05 #7

aundro wrote:
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

Oct 24 '05 #8
"Julian Turner" <ju****@baconbutty.com> writes:
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;
})();
Hello,

that seems to be the solution that matches best what I had in mind.

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.

ok, I got it. Thanks!

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

Julian


Thank you much for your suggestion. The above solution is pretty
satisfactory :)

Best regards,
Arnaud
--
Arnaud DIEDEREN
Software Developer
IONIC Software
Rue de Wallonie, 18 - 4460 Grace-Hollogne - Belgium
Tel: +32.4.3640364 - Fax: +32.4.2534737
mailto:ad@ionicsoft.com
http://www.ionicsoft.com
Oct 25 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Maximus | last post: by
8 posts views Thread by Daniel Billingsley | last post: by
3 posts views Thread by Eric the half a Bee | last post: by
40 posts views Thread by Mark P | last post: by
8 posts views Thread by Andrew Robinson | last post: by
3 posts views Thread by Doug | last post: by
11 posts views Thread by =?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?= | last post: by
12 posts views Thread by Matt B | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.