juan_gandhi: (Default)
[personal profile] juan_gandhi

for (var idx in ['uno', 'dos', 'tres']) {
  println (typeof idx + " " + idx);
}


string 0
string 1
string 2



Explanation
JavaScript does not have arrays. Period. What it has is an imitation of arrays. Namely, objects that have keys (all keys in objects are strings) that represent numbers.

var x = ['uno', 'dos', 'tres'];
x[3.999] = 'casi cuatro';
x[-1] = 'minus uno';
println(x.length);
for (var idx in x) {
  println(idx + "->" + x[idx]);
}

(try it on testbed)

You can pass anything as an index; it is converted to a string.

So, I guess, iterables or streams or continuations should be implemented in JavaScript without using arrays.

Date: 2008-11-10 03:59 am (UTC)
From: [identity profile] ygam.livejournal.com
Интересно, а "эк", "до", "тин" он понимает?

Date: 2008-11-10 07:43 am (UTC)
From: [personal profile] alll
Ессно. Он перебирает индекс в массиве, а не значения.

Date: 2008-11-10 04:09 am (UTC)
From: [identity profile] spamsink.livejournal.com
Это с каким locale?

Date: 2008-11-10 04:33 am (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
О, я гляжу, пример ещё более confusing, чем можно было бы придумать с ходу, ха!

Смотри, речь идёт о том, что массивов-то в джаваскрипте нету! А есть их имитация.

Date: 2008-11-10 05:23 am (UTC)
From: [identity profile] spamsink.livejournal.com
Я понятия не имею, не только какого типа оказывается idx, но и как у него работает разыменование по умолчанию.

Date: 2008-11-11 03:16 am (UTC)
From: [identity profile] spamsink.livejournal.com
Смотри, речь идёт о том, что массивов-то в джаваскрипте нету! А есть их имитация.

Это я знаю. Но почему я должен догадываться, что (var idx in x) перебирает ключи? Когда хотят имитировать массивы, обычно хотят (по умолчанию) значения перебирать.
Edited Date: 2008-11-11 03:16 am (UTC)

Date: 2008-11-11 04:03 am (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
А для этого в этом смешном языке есть for each (var value in array) {}, но это только в седьмой версии, которую ещё никто не знает...

Date: 2008-11-11 08:45 am (UTC)
From: [identity profile] spamsink.livejournal.com
А чтобы индуктивная переменная была tuple, по принципу "вы этого хотели, вот вам" "ты скажи, ты скажи, чё те надо, чё те надо - я те дам, я те дам, чё ты хошь" - можно?

Date: 2008-11-11 04:44 pm (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
Мне кажется, это главная проблема джаваскрипта. Там нету tuples.

Date: 2008-11-11 05:02 pm (UTC)

Date: 2008-11-10 07:28 am (UTC)
From: [identity profile] pechkin.livejournal.com
Уйдя с perlа, я думал, что немного отдохну от интересно устроенных языков...

Date: 2008-11-10 08:16 am (UTC)
From: [identity profile] cema.livejournal.com
Комментарии хорошо говорят о языке. :-) То есть точно говорят, а не хорошо.

Date: 2008-11-10 09:21 am (UTC)
From: [identity profile] pigmeich.livejournal.com
Комменты к записи -- зачетные. Как и к предыдущей про JS.

Date: 2008-11-10 12:24 pm (UTC)
From: [identity profile] ex-chrobin.livejournal.com
в экшнскрипте еще хуже, приходит именно что инт, который кунеймом не является

Date: 2008-11-10 03:57 pm (UTC)
From: [identity profile] selfmade.livejournal.com
numeric-associative objerrey

var a = ['uno', 'dos'];
a.zero = 'zero';
a['2'] = 'tres';
alert(a.length);
for (var idx in a) {
alert(typeof idx + " " + idx);
}

http://andrewdupont.net/2006/05/18/javascript-associative-arrays-considered-harmful/

Date: 2008-11-10 04:17 pm (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
Harmful, на самом деле, "обычные массивы" - т.к. их в джаваскрипте нету. Они там имитируются, для обмана зрения джавщиков.

А этот Дюпон просто не въезжает.

Date: 2008-11-10 04:40 pm (UTC)
From: [identity profile] selfmade.livejournal.com
Ну правильно, потому что они имплементированы через associative array of properties of object.

Date: 2008-11-10 06:40 pm (UTC)
nine_k: A stream of colors expanding from brain (Default)
From: [personal profile] nine_k
I hate this convention, too, as well as several others.
I'd rather have Python or Lua as browser built-in language. No chance, though.

Date: 2008-11-11 04:45 pm (UTC)
From: [identity profile] ivan-gandhi.livejournal.com
Well, at least we should be happy that JS4 standard was not adopted. It was turning JS into a strictly-typed language.

Date: 2008-11-11 05:57 pm (UTC)
nine_k: A stream of colors expanding from brain (Default)
From: [personal profile] nine_k
Sure.
JS is conceptually fine, on the lower level. But its syntax is not the best of breed. (I wonder if JS could have a documented "VM" level for plugging in different syntaxes.)

Profile

juan_gandhi: (Default)
Juan-Carlos Gandhi

July 2025

S M T W T F S
  12345
6789 1011 12
13141516171819
20212223242526
2728293031  

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 13th, 2025 02:49 am
Powered by Dreamwidth Studios