syntax for assigning to an array
|
|
Thread rating:  |
miken32 - 27 Nov 2007 19:52 GMT 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.
Randy Webb - 27 Nov 2007 19:58 GMT 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: [quoted text clipped - 6 lines] > > [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.
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
miken32 - 27 Nov 2007 20:58 GMT > miken32 said the following on 11/27/2007 2:52 PM: > [quoted text clipped - 37 lines] > 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.
Kailash Nadh - 27 Nov 2007 21:45 GMT > > miken32 said the following on 11/27/2007 2:52 PM: > [quoted text clipped - 42 lines] > 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
Richard Cornford - 27 Nov 2007 22:09 GMT >> miken32 said the following on 11/27/2007 2:52 PM: >> [quoted text clipped - 4 lines] >>> 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.
Randy Webb - 27 Nov 2007 22:19 GMT 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?
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Richard Cornford - 27 Nov 2007 23:12 GMT > Richard Cornford said the following on 11/27/2007 5:09 PM: > <snip> [quoted text clipped - 10 lines] > 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.
Randy Webb - 27 Nov 2007 22:39 GMT miken32 said the following on 11/27/2007 3:58 PM:
>> miken32 said the following on 11/27/2007 2:52 PM: >> [quoted text clipped - 28 lines] > var a, b, c, d = 'one two three'; > [a, b, c] = d.split(' '); <sigh> I 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.
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
miken32 - 27 Nov 2007 22:57 GMT > miken32 said the following on 11/27/2007 3:58 PM: > [quoted text clipped - 52 lines] > 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.
Randy Webb - 27 Nov 2007 23:25 GMT miken32 said the following on 11/27/2007 5:57 PM:
<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?
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
-Lost - 28 Nov 2007 05:08 GMT Response to Randy Webb <HikksNotAtHome@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 [quoted text clipped - 3 lines] > 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.
 Signature -Lost Remove the extra words to reply by e-mail. Don't e-mail me. I am kidding. No I am not.
Randy Webb - 28 Nov 2007 05:21 GMT -Lost said the following on 11/28/2007 12:08 AM:
> Response to Randy Webb <HikksNotAtHome@aol.com>: > [quoted text clipped - 21 lines] > > 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.
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Bruno Desthuilliers - 28 Nov 2007 13:02 GMT 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 !-)
beegee - 29 Nov 2007 14:51 GMT On Nov 28, 8:02 am, Bruno Desthuilliers <bruno. 42.desthuilli...@wtf.websiteburo.oops.com> wrote:
> Python has a similar (yet a bit cleaner IMHO) concept named 'tuple > unpacking', which let you emulate "multiple return values": [quoted text clipped - 3 lines] > > 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
Michael White - 27 Nov 2007 23:10 GMT >>miken32 said the following on 11/27/2007 2:52 PM: >> [quoted text clipped - 46 lines] > > May just be a problem with my browser's JS engine I guess. No, it's a problem with your syntax. I guess. Mick
slebetman - 27 Nov 2007 22:57 GMT > In PHP, if a function returns an array it's fairly common to capture > its return values like this: [quoted text clipped - 6 lines] > > [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');
Randy Webb - 27 Nov 2007 23:27 GMT slebetman said the following on 11/27/2007 5:57 PM:
>> In PHP, if a function returns an array it's fairly common to capture >> its return values like this: [quoted text clipped - 22 lines] > 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
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Randy Webb - 28 Nov 2007 05:22 GMT 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];
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
slebetman - 28 Nov 2007 09:11 GMT > Randy Webb said the following on 11/27/2007 6:27 PM: > [quoted text clipped - 11 lines] > > 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'); }
Randy Webb - 28 Nov 2007 10:21 GMT slebetman said the following on 11/28/2007 4:11 AM:
>> Randy Webb said the following on 11/27/2007 6:27 PM: >> [quoted text clipped - 10 lines] > > 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.
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
slebetman - 28 Nov 2007 16:14 GMT > slebetman said the following on 11/28/2007 4:11 AM: > [quoted text clipped - 22 lines] > > 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; [quoted text clipped - 21 lines] > > 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>
Randy Webb - 29 Nov 2007 00:48 GMT slebetman said the following on 11/28/2007 11:14 AM:
>> slebetman said the following on 11/28/2007 4:11 AM: <snip>
>> function testme () { >> var x; [quoted text clipped - 7 lines] > 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.
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
slebetman - 29 Nov 2007 04:00 GMT > slebetman said the following on 11/28/2007 11:14 AM: > [quoted text clipped - 23 lines] > > 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.
Thomas 'PointedEars' Lahn - 27 Nov 2007 23:48 GMT >> In PHP, if a function returns an array it's fairly common to capture >> its return values like this: [quoted text clipped - 9 lines] > 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/New_in_JavaScript_1.7#Destructuring_assignment
PointedEars
VK - 28 Nov 2007 16:31 GMT 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" and start parsing <script> blocks by the rules of such language. No one language spec goes so far - and for some good I guess. Other words one may add someVeryCoolMethod to Global and it's OK, just document it properly. From the other side one cannot say allow "this" or constructor itself to be in the left-hand side of assignment just because someone's hands are overly itchy tonight. For itchy hands of this kind there is project manager to hit by his stick on time. Starting from Gecko JavaScript 1.6 and especially 1.7 I'm getting more and more disappointed by the engine project manager up to the point to start wondering if there is one at all...
> http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7#Destructuring_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**".
VK - 28 Nov 2007 20:10 GMT > http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7#Destructuring_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**". http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7#Using_JavaScript_1.7
"In order to use some of the new features of JavaScript 1.7, you need to specify that you wish to use JavaScript 1.7. In HTML or XUL code, use: <script type="application/javascript;version=1.7"/> "
Uhm... Did we use this type? I see that not but 1.7 is still in all its "beauty"
I hope Mozilla at least will issue some small warning at the day then they will decide to remove .prototype property from objects or something equally revolutionary.
Thomas 'PointedEars' Lahn - 29 Nov 2007 20:24 GMT > http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7#Using_JavaScript_1.7 > [quoted text clipped - 8 lines] > > [rant] If you had any clue what you are writing about, you would have known that it is not necessarily to declare the language version for that particular feature as it does not use a new keyword. Unsurprisingly, the above resource documents this.
PointedEars
 Signature "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>
slebetman - 28 Nov 2007 23:33 GMT > On Nov 28, 2:48 am, Thomas 'PointedEars' Lahn <PointedE...@web.de> > wrote: [quoted text clipped - 5 lines] > 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).
Thomas 'PointedEars' Lahn - 29 Nov 2007 20:40 GMT >> On Nov 28, 2:48 am, Thomas 'PointedEars' Lahn <PointedE...@web.de> >> wrote: [quoted text clipped - 6 lines] > 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
 Signature "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>
Randy Webb - 30 Nov 2007 04:50 GMT Thomas 'PointedEars' Lahn said the following on 11/29/2007 3:40 PM:
>>> On Nov 28, 2:48 am, Thomas 'PointedEars' Lahn <PointedE...@web.de> >>> wrote: [quoted text clipped - 7 lines] > > Not quite. JavaScript 1.7 came first. Another case of ECMA documenting what was already in place. Nice.
 Signature Randy Chance Favors The Prepared Mind comp.lang.javascript FAQ - http://jibbering.com/faq/index.html Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Thomas 'PointedEars' Lahn - 29 Nov 2007 19:52 GMT > On Nov 28, 2:48 am, Thomas 'PointedEars' Lahn <PointedE...@web.de> > wrote: [quoted text clipped - 3 lines] > implementation. ECMAScript specs allow extensions and even extra > keywords. [...] Unsurprisingly, you missed the point completely.
>> http://developer.mozilla.org/en/docs/New_in_JavaScript_1.7#Destructuring_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
VK - 29 Nov 2007 20:22 GMT On Nov 29, 10:52 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> wrote:
>>http://developer.mozilla.org/en/docs/New_in_JavaScript_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". 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. No code is executed, you have to look the error console for the message. So I don't care what is written in JavaScript1.7 changes, but the real thought of the author of these changes should be close to what I said and quoted again at the top of this message.
<html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <script> var a,b,c; try { [a,b,c] = [1,2,3]; window.alert(a); } catch(e) { window.alert('[a,b,c] '+e.message); }
try { eval("new Array(a,b,c) = new Array(1,2,3)"); window.alert(a); } catch(e) { window.alert('new Array '+e.message); }
//try { // new Array(a,b,c) = new Array(1,2,3); // window.alert(a); //} //catch(e) { // window.alert('new Array '+e.message); //} </script> </head> <body> </body> </html>
Thomas 'PointedEars' Lahn - 29 Nov 2007 20:31 GMT > On Nov 29, 10:52 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> > wrote: [quoted text clipped - 6 lines] > 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
 Signature realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann
VK - 29 Nov 2007 20:41 GMT 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.
Thomas 'PointedEars' Lahn - 29 Nov 2007 20:50 GMT > On Nov 29, 11:31 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> > wrote: [quoted text clipped - 5 lines] > 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
 Signature "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>
Thomas 'PointedEars' Lahn - 29 Nov 2007 20:46 GMT >> 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
VK - 29 Nov 2007 21:49 GMT > 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, and the other called it "destructuring assignment" and the job is done... That is my point: the *core* of the language - any language, not just JavaScript - is not some external interface to play with. It is a very serious deal. It still can be changed to meet burning evident modern demands, but it is definitely not something to silently and anonymously break and then comment in one paragraph of text in some corporate wiki. Once there were discussion here over non-standard upon ECMA 3ed ed. handling of function-expressions. Now anyone is entitled to break the core of the language as long as it's named beautifully?
Thomas 'PointedEars' Lahn - 29 Nov 2007 22:14 GMT >> 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/presentations/xtech2006/javascript/
PointedEars
 Signature realism: HTML 4.01 Strict evangelism: XHTML 1.0 Strict madness: XHTML 1.1 as application/xhtml+xml -- Bjoern Hoehrmann
VK - 30 Nov 2007 16:28 GMT On Nov 30, 1:14 am, Thomas 'PointedEars' Lahn <PointedE...@web.de> wrote:
> ,-[ECMAScript Ed. 1 to 3 Final] > | [quoted text clipped - 5 lines] > > http://developer.mozilla.org/presentations/xtech2006/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/publications/files/ECMA-ST/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."
Thomas 'PointedEars' Lahn - 30 Nov 2007 16:54 GMT > On Nov 30, 1:14 am, Thomas 'PointedEars' Lahn <PointedE...@web.de> > wrote: [quoted text clipped - 11 lines] > 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/publications/files/ECMA-ST/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
 Signature var bugRiddenCrashPronePieceOfJunk = ( navigator.userAgent.indexOf('MSIE 5') != -1 && navigator.userAgent.indexOf('Mac') != -1 ) // Plone, register_function.js:16
VK - 30 Nov 2007 17:19 GMT On Nov 30, 7:54 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> wrote:
> > On Nov 30, 1:14 am, Thomas 'PointedEars' Lahn <PointedE...@web.de> > > wrote: [quoted text clipped - 13 lines] > > 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.
Thomas 'PointedEars' Lahn - 30 Nov 2007 18:58 GMT > On Nov 30, 7:54 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> > wrote: [quoted text clipped - 16 lines] > 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
 Signature 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, <f806at$ail$1$8300dec7@news.demon.co.uk>
Bruno Desthuilliers - 28 Nov 2007 12:55 GMT miken32 a écrit :
> 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(); > ?> <ot> For those that don't know PHP:
list($a, $b, $c) = array(1, 2, 3);
is equivalent to:
$x = array(1, 2, 3); $a = $x[0]; $b = $x[1]; $c = $x[2];
</ot>
> In Javascript, would the equivalent (acceptable) code be this? > > [foo, bar, baz] = some_function_that_return_an_array(); No. There's no such thing in javascript. Anyway, if the ordering in your array is signifiant (ie: your array is really a tuple, IOW an index-based record), then you should consider returning an object instead:
var obj = someFuncThatReturnsAnObj(); alert(obj.foo + "\n" + obj.bar +"\n" + obj.baaz);
Even if you can't modify some_function_that_return_an_array, you can at least wrap it:
function someFuncThatReturnsAnObj() { var ar = some_function_that_return_an_array(); return { foo: ar[0], bar: ar[1], baaz: ar[2] }; }
HTH
VK - 28 Nov 2007 14:49 GMT > In PHP, if a function returns an array it's fairly common to capture > its return values like this: [quoted text clipped - 6 lines] > > [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.
slebetman - 28 Nov 2007 16:03 GMT > > 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; [quoted text clipped - 11 lines] > 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.
Thomas 'PointedEars' Lahn - 29 Nov 2007 20:37 GMT >> In PHP, if a function returns an array it's fairly common to capture >> its return values like this: [quoted text clipped - 8 lines] > > [args] in JavaScript is an implicit Array constructor call. In this case it is not, but rather a list.
> Constructor call cannot be left-hand side of the assignment, It is not an implicit constructor call there.
> [rant] Probably someone already told you that you have no clue what you are writing about.
> Evidently some of Gecko JavaScript engine developers came from Perl/ > PHP grounds It is much more likely that this feature was borrowed from Python. Brendan Eich explicitly referred to implementing features in JavaScript like they are in Python in his blog. That also includes Array comprehensions which have also been introduced with JavaScript 1.7.
> so it just added a feature he used so do to not be > bothered with language/standards differences. The usual utter VK nonsense.
PointedEars
 Signature Anyone who slaps a 'this page is best viewed with Browser X' label on a Web page appears to be yearning for the bad old days, before the Web, when you had very little chance of reading a document written on another computer, another word processor, or another network. -- Tim Berners-Lee
|
|
|