By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,945 Members | 1,648 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,945 IT Pros & Developers. It's quick & easy.

syntax for assigning to an array

P: n/a
In PHP, if a function returns an array it's fairly common to capture
its return values like this:

<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>

In Javascript, would the equivalent (acceptable) code be this?

[foo, bar, baz] = some_function_that_return_an_array();

I usually run my code through JSLint as a quick sanity check, but this
expression makes it die. Clearly it's a parsing bug in JSLint, but I
also wanted to make sure that I wasn't doing something completely off
the wall. Thanks.
Nov 27 '07 #1
Share this Question
Share on Google+
37 Replies


P: n/a
miken32 said the following on 11/27/2007 2:52 PM:
In PHP, if a function returns an array it's fairly common to capture
its return values like this:

<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>

In Javascript, would the equivalent (acceptable) code be this?

[foo, bar, baz] = some_function_that_return_an_array();
var theArray = some_function_that_return_an_array();
alert('theArray is now an array')
I usually run my code through JSLint as a quick sanity check, but this
expression makes it die.
It doesn't die, it generates a message:

Problem at line 1 character 2: Expected a JSON value.
Clearly it's a parsing bug in JSLint,
No it isn't.
but I also wanted to make sure that I wasn't doing something
completely off the wall.
You aren't even in the house to be off the wall :)
Thanks.
Welcome.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 27 '07 #2

P: n/a
On Nov 27, 12:58 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
miken32 said the following on 11/27/2007 2:52 PM:
In PHP, if a function returns an array it's fairly common to capture
its return values like this:
<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>
In Javascript, would the equivalent (acceptable) code be this?
[foo, bar, baz] = some_function_that_return_an_array();

var theArray = some_function_that_return_an_array();
alert('theArray is now an array')
I usually run my code through JSLint as a quick sanity check, but this
expression makes it die.

It doesn't die, it generates a message:

Problem at line 1 character 2: Expected a JSON value.
Clearly it's a parsing bug in JSLint,

No it isn't.
but I also wanted to make sure that I wasn't doing something
completely off the wall.

You aren't even in the house to be off the wall :)
Thanks.

Welcome.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/
I'm using Safari, and if I try to validate this code:

var a, b, c, d = 'one two three';
[a, b, c] = d.split(' ');

I get "Problem at line 151 character 13: Undefined value"

May just be a problem with my browser's JS engine I guess.
Nov 27 '07 #3

P: n/a
On Nov 27, 8:58 pm, miken32 <mike...@gmail.comwrote:
On Nov 27, 12:58 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
miken32 said the following on 11/27/2007 2:52 PM:
In PHP, if a function returns an array it's fairly common to capture
its return values like this:
<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>
In Javascript, would the equivalent (acceptable) code be this?
[foo, bar, baz] = some_function_that_return_an_array();
var theArray = some_function_that_return_an_array();
alert('theArray is now an array')
I usually run my code through JSLint as a quick sanity check, but this
expression makes it die.
It doesn't die, it generates a message:
Problem at line 1 character 2: Expected a JSON value.
Clearly it's a parsing bug in JSLint,
No it isn't.
but I also wanted to make sure that I wasn't doing something
completely off the wall.
You aren't even in the house to be off the wall :)
Thanks.
Welcome.
--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/

I'm using Safari, and if I try to validate this code:

var a, b, c, d = 'one two three';
[a, b, c] = d.split(' ');
[] is for carrying the values of an array really.
eg: var a = [1,2,3];

I don't think there is a shorthand for what you are trying to do,
unless ofcourse, you'd use a[0], [1].. instead of a,b,c.. OR assigning
the values manually to the variables after the split()
>
I get "Problem at line 151 character 13: Undefined value"

May just be a problem with my browser's JS engine I guess.
--
Kailash Nadh | http://kailashnadh.name
Nov 27 '07 #4

P: n/a
"miken32" wrote:
On Nov 27, 12:58 pm, Randy Webb wrote:
>miken32 said the following on 11/27/2007 2:52 PM:
>>In PHP, if a function returns an array it's fairly
common to capture its return values like this:
>><?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>
You have posted this PHP to a javascript group without any explanation as to what it is actually
doing. It is not very relational to assume that people who know javascript will also know PHP.
Would you make that assumption the other way around? Saying things like "capture its return
values" doesn't really help. In javascript terms a function/method call that "returns an array"
returns a single value and that value is the array object. If you want that array you assign the
value to a variable or the property of an object, giving you a variable or property of an object
that refers to an array object.

<snip>
I'm using Safari, and if I try to validate this code:
"Validate"? Do you mean execute?
var a, b, c, d = 'one two three';
[a, b, c] = d.split(' ');
That last line is just a syntax error. The - [a, b, c] - is an Array literal (defines an array
object with 3 elements containing, in tern, the values of the local variables a, b and c (which
are all the undefined value at this point)). The evaluation of the Array literal results in a
value, and there is no sense in which assigning another value to a value can work, and it is
also forbidden by the syntax rules.
I get "Problem at line 151 character 13: Undefined value"
But don't intend letting on to us which line line 151 is.
May just be a problem with my browser's JS engine I guess.
An error is inevitable because of the syntax error in the code, which error (in terms of the
exact wording) will depend on the JS engine. But I think the error you report is the last error
generated, while it is the first error generate that should always be the first error addressed
(as all subsequent errors may be a direct consequence of the first).

Richard.

Nov 27 '07 #5

P: n/a
Richard Cornford said the following on 11/27/2007 5:09 PM:

<snip>
An error is inevitable because of the syntax error in the code, which
error (in terms of the exact wording) will depend on the JS engine. But
I think the error you report is the last error generated, while it is
the first error generate that should always be the first error addressed
(as all subsequent errors may be a direct consequence of the first).
Typically, and in experience, the first error reported is usually the
first error encountered. Do you know of a UA that will report the last
error encountered instead of the first error?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 27 '07 #6

P: n/a
miken32 said the following on 11/27/2007 3:58 PM:
On Nov 27, 12:58 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
>miken32 said the following on 11/27/2007 2:52 PM:
>>In PHP, if a function returns an array it's fairly common to capture
its return values like this:
<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>
In Javascript, would the equivalent (acceptable) code be this?
[foo, bar, baz] = some_function_that_return_an_array();
var theArray = some_function_that_return_an_array();
alert('theArray is now an array')
>>I usually run my code through JSLint as a quick sanity check, but this
expression makes it die.
It doesn't die, it generates a message:

Problem at line 1 character 2: Expected a JSON value.
>>Clearly it's a parsing bug in JSLint,
No it isn't.
>>but I also wanted to make sure that I wasn't doing something
completely off the wall.
You aren't even in the house to be off the wall :)
>>Thanks.
Welcome.
I'm using Safari, and if I try to validate this code:

var a, b, c, d = 'one two three';
[a, b, c] = d.split(' ');
<sighI give you code to do exactly what you want and you still try to
fubar it.

var myArray = d.split(' ');

Now, myArray is an array that you wanted. You need to stop thinking PHP
and start learning the JS way if you want to write JS.
I get "Problem at line 151 character 13: Undefined value"
GIGO.
May just be a problem with my browser's JS engine I guess.
No, it is a major problem with your code.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 27 '07 #7

P: n/a
On Nov 27, 3:39 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
miken32 said the following on 11/27/2007 3:58 PM:
On Nov 27, 12:58 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
miken32 said the following on 11/27/2007 2:52 PM:
>In PHP, if a function returns an array it's fairly common to capture
its return values like this:
<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>
In Javascript, would the equivalent (acceptable) code be this?
[foo, bar, baz] = some_function_that_return_an_array();
var theArray = some_function_that_return_an_array();
alert('theArray is now an array')
>I usually run my code through JSLint as a quick sanity check, but this
expression makes it die.
It doesn't die, it generates a message:
Problem at line 1 character 2: Expected a JSON value.
>Clearly it's a parsing bug in JSLint,
No it isn't.
>but I also wanted to make sure that I wasn't doing something
completely off the wall.
You aren't even in the house to be off the wall :)
>Thanks.
Welcome.
I'm using Safari, and if I try to validate this code:
var a, b, c, d = 'one two three';
[a, b, c] = d.split(' ');

<sighI give you code to do exactly what you want and you still try to
fubar it.

var myArray = d.split(' ');

Now, myArray is an array that you wanted. You need to stop thinking PHP
and start learning the JS way if you want to write JS.
I get "Problem at line 151 character 13: Undefined value"

GIGO.
May just be a problem with my browser's JS engine I guess.

No, it is a major problem with your code.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/
I understand that's not the way to do it, no need for sarcasm and
sighs!

I was reposting similarly incorrect code to highlight the parsing
error in JSLint; I guess I wasn't clear enough, but the error message
was coming from JSLint, not my browser. Clearly if I'm validating 2
lines of JS and it reports an error on line 151 something's wrong, and
I'll send an email to alert him to the problem.

And I promise with all my heart that I will take the extra lines of
code to explicitly assign elements of the returned array to variables.
Thanks.
Nov 27 '07 #8

P: n/a
On Nov 28, 3:52 am, miken32 <mike...@gmail.comwrote:
In PHP, if a function returns an array it's fairly common to capture
its return values like this:

<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>

In Javascript, would the equivalent (acceptable) code be this?

[foo, bar, baz] = some_function_that_return_an_array();
As others have replied, and you have found out for yourself by trying
to run this in a browser, that's not valid javascript. The acceptable
equivalent is:

var tmp = some_function_that_return_an_array();
foo = tmp[0];
bar = tmp[1];
baz = tmp[2];

However, you can emulate a similar functionality with a Tcl style
syntax rather than PHP's Perl style syntax:

function assign (thearray) {
for (var i=1; i < arguments.length; i++) {
eval (arguments[i] + '=' + thearray[i-1])
}
}

# The syntax to use this would be:

assign(function_return_array(), 'foo', 'bar', 'baz');
Nov 27 '07 #9

P: n/a
miken32 wrote:
On Nov 27, 12:58 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
>>miken32 said the following on 11/27/2007 2:52 PM:

>>>In PHP, if a function returns an array it's fairly common to capture
its return values like this:
>>><?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>
>>>In Javascript, would the equivalent (acceptable) code be this?
>>>[foo, bar, baz] = some_function_that_return_an_array();

var theArray = some_function_that_return_an_array();
alert('theArray is now an array')

>>>I usually run my code through JSLint as a quick sanity check, but this
expression makes it die.

It doesn't die, it generates a message:

Problem at line 1 character 2: Expected a JSON value.

>>>Clearly it's a parsing bug in JSLint,

No it isn't.

>>>but I also wanted to make sure that I wasn't doing something
completely off the wall.

You aren't even in the house to be off the wall :)

>>>Thanks.

Welcome.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/


I'm using Safari, and if I try to validate this code:

var a, b, c, d = 'one two three';
[a, b, c] = d.split(' ');

I get "Problem at line 151 character 13: Undefined value"

May just be a problem with my browser's JS engine I guess.
No, it's a problem with your syntax. I guess.
Mick
Nov 27 '07 #10

P: n/a
Randy Webb wrote:
Richard Cornford said the following on 11/27/2007 5:09 PM:
<snip>
>An error is inevitable because of the syntax error in the
code, which error (in terms of the exact wording) will
depend on the JS engine. But I think the error you report
is the last error generated, while it is the first error
generate that should always be the first error addressed (as all subsequent errors may be a
direct consequence of
the first).

Typically, and in experience, the first error reported is
usually the first error encountered. Do you know of a UA
that will report the last error encountered instead of
the first error?
I know of at leas some browsers where the report of one error is replaced by the report of the
next, and so when everything grinds to a halt and someone looks at the errors reported they are
actually looking at the last error (and usually an active 'back' button that can be used to look
at the previous errors).

The syntax error in the code posted would not produce the error reported, but the error reported
could easily be the consequence of the syntax error stopping the compiling of all the SCRIPT
contents where it appeared.

Richard.

Nov 27 '07 #11

P: n/a
miken32 said the following on 11/27/2007 5:57 PM:
On Nov 27, 3:39 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
<snip>
I understand that's not the way to do it, no need for sarcasm and
sighs!
Fair enough.
I was reposting similarly incorrect code to highlight the parsing
error in JSLint; I guess I wasn't clear enough, but the error message
was coming from JSLint, not my browser.
My misunderstanding was that I read it as you saying JSLint had a
problem when it is merely reporting errors in your code.
Clearly if I'm validating 2 lines of JS and it reports an error
on line 151 something's wrong, and I'll send an email to alert
him to the problem.
When I try the code you posted in JSLint, I get this error message:

Problem at line 2 character 13: 'value' is null or not an object

So not sure why JSLint is telling you line 151 but Douglas is definitely
the person to email/talk to about it.
And I promise with all my heart that I will take the extra lines of
code to explicitly assign elements of the returned array to variables.
What exactly does that code do in PHP? Set a to the first element, b to
the second element and c to the third element after d is split?

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 27 '07 #12

P: n/a
slebetman said the following on 11/27/2007 5:57 PM:
On Nov 28, 3:52 am, miken32 <mike...@gmail.comwrote:
>In PHP, if a function returns an array it's fairly common to capture
its return values like this:

<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>

In Javascript, would the equivalent (acceptable) code be this?

[foo, bar, baz] = some_function_that_return_an_array();

As others have replied, and you have found out for yourself by trying
to run this in a browser, that's not valid javascript. The acceptable
equivalent is:

var tmp = some_function_that_return_an_array();
foo = tmp[0];
bar = tmp[1];
baz = tmp[2];

However, you can emulate a similar functionality with a Tcl style
syntax rather than PHP's Perl style syntax:

function assign (thearray) {
for (var i=1; i < arguments.length; i++) {
eval (arguments[i] + '=' + thearray[i-1])
arguments[i] = thearray[i-1]

http://jibbering.com/faq/index.html#FAQ4_40

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 27 '07 #13

P: n/a
slebetman wrote:
On Nov 28, 3:52 am, miken32 <mike...@gmail.comwrote:
>In PHP, if a function returns an array it's fairly common to capture
its return values like this:

<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>

In Javascript, would the equivalent (acceptable) code be this?

[foo, bar, baz] = some_function_that_return_an_array();

As others have replied, and you have found out for yourself by trying
to run this in a browser, that's not valid javascript. [...]
That depends how you understand "javascript". JavaScript 1.7 as implemented
in Gecko 1.8.1 (Firefox 2 etc.) allows this:

http://developer.mozilla.org/en/docs...ing_assignment
PointedEars
Nov 27 '07 #14

P: n/a
Response to Randy Webb <Hi************@aol.com>:
>And I promise with all my heart that I will take the extra lines
of code to explicitly assign elements of the returned array to
variables.

What exactly does that code do in PHP? Set a to the first element,
b to the second element and c to the third element after d is
split?
It is a language construct that iterates an array into respective
variables consecutively.

Basically a native function (Object) that accepts an Array object and
a series of arguments, then iterates the Array's contents into those
variables whose name is specified by arguments.

It also makes some common assumptions in regards to array handling in
PHP. Primarily that PHP can create true associative arrays and list
() cannot handle that scenario natively. It also assumes that the
data requested to initialize the arrays begins at an indice of 0.

I don't believe I've ever seen anything remotely possible like this
in JavaScript.

fnLikeAVarStatement(a, b, c) = objArray; //or any valid Array object.

--
-Lost
Remove the extra words to reply by e-mail. Don't e-mail me. I am
kidding. No I am not.
Nov 28 '07 #15

P: n/a
-Lost said the following on 11/28/2007 12:08 AM:
Response to Randy Webb <Hi************@aol.com>:
>>And I promise with all my heart that I will take the extra lines
of code to explicitly assign elements of the returned array to
variables.
What exactly does that code do in PHP? Set a to the first element,
b to the second element and c to the third element after d is
split?

It is a language construct that iterates an array into respective
variables consecutively.

Basically a native function (Object) that accepts an Array object and
a series of arguments, then iterates the Array's contents into those
variables whose name is specified by arguments.

It also makes some common assumptions in regards to array handling in
PHP. Primarily that PHP can create true associative arrays and list
() cannot handle that scenario natively. It also assumes that the
data requested to initialize the arrays begins at an indice of 0.

I don't believe I've ever seen anything remotely possible like this
in JavaScript.

fnLikeAVarStatement(a, b, c) = objArray; //or any valid Array object.
And all this time, I just referred to the entry in the array I wanted. I
could see a possible benefit of it but not entirely sure I would ever
use it in JS. But thank you.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 28 '07 #16

P: n/a
Randy Webb said the following on 11/27/2007 6:27 PM:
slebetman said the following on 11/27/2007 5:57 PM:
<snip>
>function assign (thearray) {
for (var i=1; i < arguments.length; i++) {
eval (arguments[i] + '=' + thearray[i-1])

arguments[i] = thearray[i-1]
Although that should be:

window[arguments[i]] = thearray[i-1];

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 28 '07 #17

P: n/a
On Nov 28, 1:22 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
Randy Webb said the following on 11/27/2007 6:27 PM:
slebetman said the following on 11/27/2007 5:57 PM:

<snip>
function assign (thearray) {
for (var i=1; i < arguments.length; i++) {
eval (arguments[i] + '=' + thearray[i-1])
arguments[i] = thearray[i-1]

Although that should be:

window[arguments[i]] = thearray[i-1];
That only works for global variables. Wouldn't work for local
variables. Using 'eval' is the only way I can think of doing it:

function testme () {
var x;
var y;
assign([1,2,3,4],'x','y');
}
Nov 28 '07 #18

P: n/a
slebetman said the following on 11/28/2007 4:11 AM:
On Nov 28, 1:22 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
>Randy Webb said the following on 11/27/2007 6:27 PM:
>>slebetman said the following on 11/27/2007 5:57 PM:
<snip>
>>>function assign (thearray) {
for (var i=1; i < arguments.length; i++) {
eval (arguments[i] + '=' + thearray[i-1])
arguments[i] = thearray[i-1]
Although that should be:

window[arguments[i]] = thearray[i-1];

That only works for global variables.
True, but, it is reliable. See below.
Wouldn't work for local variables.
True again.
Using 'eval' is the only way I can think of doing it:
Did you test it? FF2, IE7, Opera9 all give undefined for the alerts below.
function testme () {
var x;
var y;
assign([1,2,3,4],'x','y');
alert(x)// undefined
alert(y)// undefined
}
Test code:

function assign (thearray) {
for (var i=1; i < arguments.length; i++) {
eval (arguments[i] + '=' + thearray[i-1])
}
}
function testme () {
var x;
var y;
assign([1,2,3,4],'x','y');
alert(x)
alert(y)
}
testme()

Although, to be honest and fair, I did not think about it in terms of
being called from within a function, rather I was thinking of it being
called from a global scope. Makes it even more reason to simply define
them yourself:

var x = someArray[0]
var y = someArray[1]

Which is the way I have always done it and thus never even considered a
way to do it differently.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 28 '07 #19

P: n/a
Randy Webb a écrit :
(snip -lost explanations about PHP's list() function)
>
And all this time, I just referred to the entry in the array I wanted. I
could see a possible benefit of it but not entirely sure I would ever
use it in JS.
<ot mode='again'>

Python has a similar (yet a bit cleaner IMHO) concept named 'tuple
unpacking', which let you emulate "multiple return values":

def some_func():
return 1, 'toto', 42

id, name, age = some_func()
It's also handy for value swapping:

a = 1
b = 2

a, b = b, a

</ot>

And FWIW, I'd certainly use it in javascript too if it was available !-)
Nov 28 '07 #20

P: n/a
VK
On Nov 27, 10:52 pm, miken32 <mike...@gmail.comwrote:
In PHP, if a function returns an array it's fairly common to capture
its return values like this:

<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>

In Javascript, would the equivalent (acceptable) code be this?

[foo, bar, baz] = some_function_that_return_an_array();
[args] in JavaScript is an implicit Array constructor call.
Constructor call cannot be left-hand side of the assignment, so this
PHP feature borrowed from Perl is not available in javascript: or so I
thought till now...

In this code snippet:
var foo,bar,baz;
try {
[foo, bar, baz] = [1,2,3];
alert(foo);
}
catch(e) {
alert(e.number & 0xFFFF);
}

IE reports - totally correct - run-time error error 5008 "Illegal
assignment".

To my huge surprise Firefox is all happy with that with foo, bar, baz
initiated with the respective values of the anonymous array, just like
there is not ECMA anymore but all Perl around.

It is funny that for say
new Array('foo') = new Array(1);
Gecko gets back the reality/standards and reports "illegal assignment
left-hand side". Well, thanks for that at least...

Evidently some of Gecko JavaScript engine developers came from Perl/
PHP grounds so it just added a feature he used so do to not be
bothered with language/standards differences.
Nov 28 '07 #21

P: n/a
On Nov 28, 10:49 pm, VK <schools_r...@yahoo.comwrote:
On Nov 27, 10:52 pm, miken32 <mike...@gmail.comwrote:
In PHP, if a function returns an array it's fairly common to capture
its return values like this:
<?php
list($foo, $bar, $baz) = some_function_that_return_an_array();
?>
[snip]
>
In this code snippet:
var foo,bar,baz;
try {
[foo, bar, baz] = [1,2,3];
alert(foo);
}
catch(e) {
alert(e.number & 0xFFFF);
}

IE reports - totally correct - run-time error error 5008 "Illegal
assignment".

To my huge surprise Firefox is all happy with that with foo, bar, baz
initiated with the respective values of the anonymous array,
Which is also totally correct (read on..)
>
Evidently some of Gecko JavaScript engine developers came from Perl/
PHP grounds so it just added a feature he used so do to not be
bothered with language/standards differences.
Not really from Perl. (OK, yes from Perl but it's valid Javascript).
As Thomas mentioned it's actually from Javascript 1.7 / ECMAScript 4.
Unfortunately no other browser apart from Firefox currently implements
Javascript 1.7.
Nov 28 '07 #22

P: n/a
On Nov 28, 6:21 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
slebetman said the following on 11/28/2007 4:11 AM:
On Nov 28, 1:22 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
Randy Webb said the following on 11/27/2007 6:27 PM:
>slebetman said the following on 11/27/2007 5:57 PM:
<snip>
>>function assign (thearray) {
for (var i=1; i < arguments.length; i++) {
eval (arguments[i] + '=' + thearray[i-1])
arguments[i] = thearray[i-1]
Although that should be:
window[arguments[i]] = thearray[i-1];
That only works for global variables.

True, but, it is reliable. See below.
Wouldn't work for local variables.

True again.
Using 'eval' is the only way I can think of doing it:

Did you test it? FF2, IE7, Opera9 all give undefined for the alerts below.
Admittedly I didn't test it when I wrote it.
>
function testme () {
var x;
var y;
assign([1,2,3,4],'x','y');

alert(x)// undefined
alert(y)// undefined
}

Test code:

function assign (thearray) {
for (var i=1; i < arguments.length; i++) {
eval (arguments[i] + '=' + thearray[i-1])
}}

function testme () {
var x;
var y;
assign([1,2,3,4],'x','y');
alert(x)
alert(y)}

testme()

Hmm.. strange. I just tested it right now in IE6, Firefox, Opera9 and
Safari and it works. I don't know why it fails on your machine.

Here's my test file which works:

<script>
function assign (thearray) {
for (var i=1; i < arguments.length; i++) {
eval (arguments[i] + '=' + thearray[i-1])
}
}

function testme () {
assign([1,2,3,4],'x','y','z');
alert(y);
alert(z);
}

testme();

/* Not much different from yours so I'm
* not sure why yours isn't working.
* As for why you'd want something like
* this, well.. some people like syntactic
* sugar others have different tastes.
*/
</script>
Nov 28 '07 #23

P: n/a
On Nov 29, 12:31 am, VK <schools_r...@yahoo.comwrote:
On Nov 28, 2:48 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
That depends how you understand "javascript".

As some ECMAScript-compliant (up to the best of authors' talents)
implementation. ECMAScript specs allow extensions and even extra
keywords. At the same time it doesn't assume that someone will make a
totally different language, still call it "JavaScript"..
Not a "totally different language" per se. Javascript 1.7 only updates
Javascript to include most of the features of the latest ECMAScript
spec. This is actually fully valid ECMAScript 4. ECMAScript 4 is still
currently in draft/proposal mode but I don't see much objections to
the proposal so far so it's on track for formal approval. AFAIK the
only two languages based on ECMAScript that have been updated to
include ECMAScript 4 features are Javascript 1.7 and Actionscript 3
(Flash/Flex, though Actionscript3 doesn't support array
destructuring).
Nov 28 '07 #24

P: n/a
slebetman said the following on 11/28/2007 11:14 AM:
On Nov 28, 6:21 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
>slebetman said the following on 11/28/2007 4:11 AM:
<snip>
>function testme () {
var x;
var y;
assign([1,2,3,4],'x','y');
alert(x)
alert(y)}

testme()


Hmm.. strange. I just tested it right now in IE6, Firefox, Opera9 and
Safari and it works. I don't know why it fails on your machine.
Because you changed the code.
function testme () {
assign([1,2,3,4],'x','y','z');
alert(y);
alert(z);
}
Compare the two. The difference in the two is monumental.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 29 '07 #25

P: n/a
On Nov 29, 8:48 am, Randy Webb <HikksNotAtH...@aol.comwrote:
slebetman said the following on 11/28/2007 11:14 AM:
On Nov 28, 6:21 pm, Randy Webb <HikksNotAtH...@aol.comwrote:
slebetman said the following on 11/28/2007 4:11 AM:

<snip>
function testme () {
var x;
var y;
assign([1,2,3,4],'x','y');
alert(x)
alert(y)}
testme()
Hmm.. strange. I just tested it right now in IE6, Firefox, Opera9 and
Safari and it works. I don't know why it fails on your machine.

Because you changed the code.
function testme () {
assign([1,2,3,4],'x','y','z');
alert(y);
alert(z);
}

Compare the two. The difference in the two is monumental.
Crap.. yeah, just realised my code actually doesn't work with local
vars as well. Sorry.. guess the OP will have to go with the more
traditional method.
Nov 29 '07 #26

P: n/a
On Nov 28, 8:02 am, Bruno Desthuilliers <bruno.
42.desthuilli...@wtf.websiteburo.oops.comwrote:
Python has a similar (yet a bit cleaner IMHO) concept named 'tuple
unpacking', which let you emulate "multiple return values":

def some_func():
return 1, 'toto', 42

id, name, age = some_func()
Ruby, same thing:

id, name, age = some_func_that_returns_an_array()

Yup, I wish it existed in Javascript too.

Bob

Nov 29 '07 #27

P: n/a
VK wrote:
On Nov 28, 2:48 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>That depends how you understand "javascript".

As some ECMAScript-compliant (up to the best of authors' talents)
implementation. ECMAScript specs allow extensions and even extra
keywords. [...]
Unsurprisingly, you missed the point completely.
>http://developer.mozilla.org/en/docs...ing_assignment

Just makes my points. "JavaScript sucks, I'm used to PHP (Perl) with
is cool, so I'll call it Destructuring_Assignment and kiss my a**".
Don't drink and post.
PointedEars
Nov 29 '07 #28

P: n/a
VK wrote:
On Nov 29, 10:52 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>>http://developer.mozilla.org/en/docs...1.7#Destructur...
Just makes my points. "JavaScript sucks, I'm used to PHP (Perl) with
is cool, so I'll call it Destructuring_Assignment and kiss my a**".

Now try this:
Evidently the interpreter thinks that new Array(a,b,c) and [a,b,c] are
all different creatures: on the eval block it gives "illegal left-hand
side assignment".
They *are* "all different creatures" here. The left-hand-side of a
destructuring assignment is not an Array object literal.
Now comment the try-catch with eval and uncomment the second try-catch
with the direct assignment. The "illegal left-hand side assignment" is
still here, but now it is a syntax error, not a run-time one anymore.
A syntax error is a type of runtime error. Again you are only demonstrating
that you have no clue at all what you are writing about. Why don't you save
everybody the trouble and stop posting here until you got a minimum clue?
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Nov 29 '07 #29

P: n/a
slebetman wrote:
On Nov 29, 12:31 am, VK <schools_r...@yahoo.comwrote:
>On Nov 28, 2:48 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>>That depends how you understand "javascript".
As some ECMAScript-compliant (up to the best of authors' talents)
implementation. ECMAScript specs allow extensions and even extra
keywords. At the same time it doesn't assume that someone will make a
totally different language, still call it "JavaScript"..

Not a "totally different language" per se. Javascript 1.7 only updates
Javascript to include most of the features of the latest ECMAScript spec.
Not quite. JavaScript 1.7 came first.
This is actually fully valid ECMAScript 4.
I would welcome that, though.
[...] AFAIK the only two languages based on ECMAScript that have been
updated to include ECMAScript 4 features are Javascript 1.7 and
Actionscript 3 (Flash/Flex, though Actionscript3 doesn't support array
destructuring).
JScript .NET is based on Netscape's first ES4 proposal.
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Nov 29 '07 #30

P: n/a
VK
On Nov 29, 11:31 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
They *are* "all different creatures" here. The left-hand-side of a
destructuring assignment is not an Array object literal.
....
A syntax error is a type of runtime error.
Poitless... Your knowledge is just low to have a reasonable
conversation.
Nov 29 '07 #31

P: n/a
Thomas 'PointedEars' Lahn wrote:
VK wrote:
>Now comment the try-catch with eval and uncomment the second try-catch
with the direct assignment. The "illegal left-hand side assignment" is
still here, but now it is a syntax error, not a run-time one anymore.

A syntax error is a type of runtime error. [...]
Please ignore that, it's nonsense. (A syntax error is a parsing error which
prevents execution.)
PointedEars
Nov 29 '07 #32

P: n/a
VK wrote:
On Nov 29, 11:31 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>They *are* "all different creatures" here. The left-hand-side of a
destructuring assignment is not an Array object literal.
...
>A syntax error is a type of runtime error.

Poitless... Your knowledge is just low to have a reasonable
conversation.
I stand corrected (by myself) for my second statement. I stand by my first
statement, though. Your quoting it and referring to it also as nonsense
shows again who lacks the basic knowledge here. Not everything that looks
like an Array object literal is parsed as such.
PointedEars
--
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>
Nov 29 '07 #33

P: n/a
VK wrote:
Thomas 'PointedEars' Lahn <PointedE...@web.dewrote:
>They *are* "all different creatures" here. The left-hand-side of a
destructuring assignment is not an Array object literal.

They are not *any more* the same things in JavaScript1.7 as they were
for the last 10 years by all official specs? and now some anonymous
author broke it for its convenience, [rant]
How stupid are you anyway?

,-[ECMAScript Ed. 1 to 3 Final]
|
| 2 Conformance
|
| [...]
| A conforming implementation of ECMAScript is permitted to support
| program [...] syntax not described in this specification.

http://developer.mozilla.org/present...06/javascript/
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Nov 29 '07 #34

P: n/a
VK
On Nov 30, 1:14 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
,-[ECMAScript Ed. 1 to 3 Final]
|
| 2 Conformance
|
| [...]
| A conforming implementation of ECMAScript is permitted to support
| program [...] syntax not described in this specification.

http://developer.mozilla.org/present...06/javascript/
Faked contract quoting, Sir: up to 5 year in jail + up to $100,000
fine + up to 100% of caused financial losses == life long out of
business :-) :-|

http://www.ecma-international.org/pu...T/Ecma-262.pdf
2 Conformance
"A conforming implementation of ECMAScript is permitted to support
program and regular expression syntax not described in this
specification. In particular, a conforming implementation of
ECMAScript is permitted to support program syntax that makes use of
the "future reserved words" listed in 7.5.3 of this specification."

Nov 30 '07 #35

P: n/a
VK wrote:
On Nov 30, 1:14 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>,-[ECMAScript Ed. 1 to 3 Final]
|
| 2 Conformance
|
| [...]
| A conforming implementation of ECMAScript is permitted to support
| program [...] syntax not described in this specification.

http://developer.mozilla.org/present...06/javascript/

Faked contract quoting, Sir: up to 5 year in jail + up to $100,000
fine + up to 100% of caused financial losses == life long out of
business :-) :-|
I could write now that you are not making any sense, but this is a *news*group.
http://www.ecma-international.org/pu...T/Ecma-262.pdf
2 Conformance
"A conforming implementation of ECMAScript is permitted to support
program and regular expression syntax not described in this
specification. [...]
Thanks for answering my rhetorical question.
PointedEars
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Nov 30 '07 #36

P: n/a
VK
On Nov 30, 7:54 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
VK wrote:
On Nov 30, 1:14 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
,-[ECMAScript Ed. 1 to 3 Final]
|
| 2 Conformance
|
| [...]
| A conforming implementation of ECMAScript is permitted to support
| program [...] syntax not described in this specification.
>http://developer.mozilla.org/present...06/javascript/
Faked contract quoting, Sir: up to 5 year in jail + up to $100,000
fine + up to 100% of caused financial losses == life long out of
business :-) :-|

I could write now that you are not making any sense, but this is a *news*group.
So what deep sense are you putting into "A conforming implementation
of ECMAScript is permitted to support program and regular expression
syntax not described in this specification." ?
As if say "@#$%^ ==== )(*&^%" could be a valid ECMAScript-compliant
statement as long as it's named somewhere some nicely, like "de-
restructuring mutable thingies" or something? Before reading and
trying to interpret really difficult matters like language specs one
need to get a feeling first what the programming is and what a
programming language is. You are not on this stage yet.
Nov 30 '07 #37

P: n/a
VK wrote:
On Nov 30, 7:54 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>VK wrote:
>>On Nov 30, 1:14 am, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
,-[ECMAScript Ed. 1 to 3 Final]
|
| 2 Conformance
|
| [...]
| A conforming implementation of ECMAScript is permitted to support
| program [...] syntax not described in this specification.
http://developer.mozilla.org/present...06/javascript/
Faked contract quoting, Sir: up to 5 year in jail + up to $100,000
fine + up to 100% of caused financial losses == life long out of
business :-) :-|
I could write now that you are not making any sense, but this is a *news*group.

So what deep sense are you putting into "A conforming implementation
of ECMAScript is permitted to support program and regular expression
syntax not described in this specification." ?
That which is most obvious to anyone but you: A destructuring assignment
is "program syntax that is not described in [the ECMAScript] specification."
JavaScript 1.7 "is permitted to support" that while being "a conforming
implementation of ECMAScript". So all your ranting that JavaScript 1.7
would break anything or that it would violate any standard is completely
baseless, of course.
PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f8*******************@news.demon.co.uk>
Nov 30 '07 #38

This discussion thread is closed

Replies have been disabled for this discussion.