473,545 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Return value of a code block


Hello all,

I was wondering whether I could do something like:

--snip--

MyType.prototyp e.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@ionic soft.com
http://www.ionicsoft.com
Oct 24 '05 #1
8 1960
aundro wrote on 24 okt 2005 in comp.lang.javas cript:
I was wondering whether I could do something like:

--snip--

MyType.prototyp e.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.prototyp e.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').substr ing(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.prototyp e.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.prototyp e.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.prototyp e.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.prototyp e.myProp = function () {var xxx = 'this is a string';
xxx.substring(4 );}();

http://www.crockford.com/javascript
Oct 24 '05 #6
"Julian Turner" <ju****@baconbu tty.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.prototyp e.myField = {
var retVal = document.create Element ("div");
retVal.style.to p = "10px";
retVal.style.le ft = "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@ionic soft.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.prototyp e.myField = {
var retVal = document.create Element ("div");
retVal.style.to p = "10px";
retVal.style.le ft = "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.prototyp e.myField = (function(){
var retVal = document.create Element ("div");
retVal.style.to p = "10px";
retVal.style.le ft = "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 "(Expressio n)".
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****@baconbu tty.com> writes:
Hi. The simplest solution is probably to adopt Douglas Crockford's
suggestion of using Function Expressions. Namely:-

MyType.prototyp e.myField = (function(){
var retVal = document.create Element ("div");
retVal.style.to p = "10px";
retVal.style.le ft = "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 "(Expressio n)".
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@ionic soft.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
2636
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
1845
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 return a value, so... do I just put return null; after I throw my exception in the catch block? or should I initialize the local variable to...
3
5869
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 Employee. This I can do. The problem comes if the EmpID is not found in the collection. The only way to achieve this I can think of is to throw an...
40
3098
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 fcn is a boolean and there are certain places within the inner functions where it may become apparent that the return value is false. In this case...
8
3357
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 (SqlCommand cm = new SqlCommand("ItemCount", cn)) { cm.CommandType = CommandType.StoredProcedure;
22
3996
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 receive 5 bytes buffer , I call the BeginReceive and then wait on AsyncWaitHandle.WaitOne() but it is signald imidiatly , and the next call to...
3
15405
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 following error if the xmlField 'location' doesn't exist in the xmlfile or if the xmlfile is blank?
11
3400
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 methods can fail I have to check them to be running ok, like this:
12
1464
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 (hashString == "MD5") { return System.Security.Cryptography.MD5.Create(); } else { return System.Security.Cryptography.SHA512.Create(); }
0
7479
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7669
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7439
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5987
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4962
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3468
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3450
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.