LoginSignup
15
15

More than 5 years have passed since last update.

JavaScriptの楽しい(?)ところ

Last updated at Posted at 2013-08-23

個人的に「面白い動作だなぁ」と思ったものの詰め合わせ。


(alert(1), "Hello"); 
//-> "Hello"

(alert(1), alert(2), alert(3)); 
//->左から順に実行

a = ["zero","one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"]

a[1+1]
//-> "two"
a["1"+"1"]
//->"eleven"

a = {}
a.valueOf = 5
a + 3
//->"[object Object]3"

a.valueOf = function(){ return 5}
a + 3
//-> 8

a = ["hello", "world", "hoge", "xxx", "test", "boo"];
b.valueOf = function(){ return  Math.floor(Math.random() * 5) }
a[+b]
//->"world"
a[+b]
//->"test"
a[+b]
//->"xxx"
a[+b]
//->"test"

a = ["hello", "world" , 1, 3];
b = 'join'
a[b]();
//->"hello,world,1,3"
b = 'sort';
a[b]();
//->[1, 3, "hello", "world"]


a = ["aaa", "bbb" , "ccc", "ddd"];
flag = true;
a[flag ? 'shift' : 'pop']();
//->"aaa"

flag = false;
a[flag ? 'shift' : 'pop']();
//->"ddd"

やっぱり、JavaScriptって楽しい。

15
15
5

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
15