473,385 Members | 1,506 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1951
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Maximus | last post by:
Hi, I was just wondering, is it good to use return without arguments in a void function as following: void SetMapLayer() { if( !Map ) return; layer = LAYER_MAP; }
8
by: Daniel Billingsley | last post by:
Suppose I have a method that returns some type of object, and in that method I have a try...catch block and just throw my own exception when I catch one. The compiler insists that all code paths...
3
by: Eric the half a Bee | last post by:
Hello I am trying to implement a search function within a collection of Employees. I am searching for a specific EmpID number in the collection, and if it is found, I want to return the...
40
by: Mark P | last post by:
I'm implementing an algorithm and the computational flow is a somewhat deep. That is, fcn A makes many calls to fcn B which makes many calls to fcn C, and so on. The return value of the outermost...
8
by: Andrew Robinson | last post by:
Are these two equivalent? Is one better than the other? I tend to go with #1 but started wondering.... Thanks, 1: using (SqlConnection cn = new SqlConnection(DataConnection)) using...
22
by: semedao | last post by:
Hi , I am using asyc sockets p2p connection between 2 clients. when I debug step by step the both sides , i'ts work ok. when I run it , in somepoint (same location in the code) when I want to...
3
by: Doug | last post by:
Hi i have a method that returns a value public bool readxml (string xmlFilename, out string value) but I would like to catch an exception if it occurs in the method . How do i catch the...
11
by: =?Utf-8?B?Um9nZXIgVHJhbmNoZXo=?= | last post by:
Hello, I have a question about the infamous GOTO statement and the way to return a result from a sub: I have a sub that has to make some calls to external COM methods, and because these...
12
by: Matt B | last post by:
I was just wondering if there is a "best" choice from the following couple of ways of returning a value from a method: 1) private HashAlgorithm GetSpecificHashAlgorithm(string hashString){ if...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.