質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

Q&A

2回答

1702閲覧

JavaScript 「:」とは

programing_you

総合スコア12

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

0グッド

1クリップ

投稿2019/09/03 05:50

編集2019/09/03 06:04

おそらく初歩的な質問だと思いますが、疑問に感じたので質問させてください!

JavaScriptのソースコードに頻繁に現れる「:」は一体何者なんですか?
見たところ、定義文によく使われているようですが、ググっても使い方や説明のページがヒットしないので質問させていただきました!

targetcode

1 //6,7行目 2 x: 0, 3 y: 0 4 5 //10行目 6 initialize: function() { 7 8  //32行目 9   render: function() {

参考URL
「 https://codepen.io/unlikenesses/pen/JKENom 」

JavaScript

1var app = app || {}; 2 3app.Block = Backbone.Model.extend({ 4 5 defaults: { 6 x: 0, 7 y: 0 8 }, 9 10 initialize: function() { 11 var shape = this.get('shape'); 12 this.set('width', shape[0].length); 13 this.set('height', shape.length); 14 } 15 16}); 17 18app.Board = Backbone.Collection.extend({ 19 20 model: app.Block 21 22}); 23 24app.BlockView = Backbone.View.extend({ 25 26 initialize: function() { 27 var self = this; 28 this.render(); 29 this.model.on('change', _.bind(this.render, this)); 30 }, 31 32 render: function() { 33 this.clearCanvas(); 34 var self = this; 35 var shape = this.model.get('shape'); 36 var x_pos = this.model.get('x') * app.blockSize; 37 var y_pos = this.model.get('y') * app.blockSize; 38 for (var y = 0; y < shape.length; y++) { 39 var row = shape[y]; 40 for (var x = 0; x < row.length; x++) { 41 if (row[x] == 1) { 42 self.stamp(x_pos + (x * app.blockSize), y_pos + (y * app.blockSize)); 43 } 44 } 45 } 46 app.events.trigger('blockRendered'); 47 return this; 48 }, 49 50 clearCanvas: function() { 51 app.context.clearRect(0, 0, app.canvas.width, app.canvas.height); 52 53 }, 54 55 stamp: function(x, y) { 56 app.context.beginPath(); 57 app.context.rect(x, y, app.blockSize, app.blockSize); 58 app.context.lineWidth = 1; 59 app.context.strokeStyle = 'white'; 60 app.context.stroke(); 61 } 62 63}); 64 65app.BoardView = Backbone.View.extend({ 66 67 el: $('canvas'), 68 69 paused: false, 70 71 muted: false, 72 73 gameOver: false, 74 75 /* Initialize board view */ 76 initialize: function() { 77 gridView = new app.GridView(); 78 if (!app.logging) { 79 var gridWidth = $('#gridContainer').width(); 80 var boardWidth = $('#board').width(); 81 $('#gridContainer').hide(); 82 $('#board').width(boardWidth - gridWidth - 60); 83 } 84 this.collection = new app.Board(app.blocks); 85 gridView.clearGrid(); 86 gridView.logGrid(); 87 $(document).on('keydown', $.proxy(this.keyAction, this)); // http://stackoverflow.com/a/13379556/519497 88 app.events.on('pause', this.pause, this); 89 app.events.on('mute', this.mute, this); 90 app.events.on('blockRendered', gridView.drawGrid, this); 91 this.start(); 92 }, 93 94 /* Set up interval */ 95 start: function() { 96 var self = this; 97 clearInterval(app.interval); 98 app.interval = setInterval(function() { 99 self.descend(); 100 self.render(); 101 }, 800); 102 }, 103 104 /* Render board */ 105 render: function() { 106 this.collection.each(function(model) { 107 var blockView = new app.BlockView({ 108 model: model 109 }); 110 }, this); 111 }, 112 113 /* Behaviour for when a block has landed */ 114 blockLanded: function(block) { 115 this.updateGrid(); 116 this.checkGameOver(block); 117 this.checkCompleteRows(); 118 this.clearCollection(); 119 this.spawnNewBlock(); 120 }, 121 122 /* Clear colletion of current block */ 123 clearCollection: function() { 124 this.collection.reset(); 125 }, 126 127 /* Create a new block at random and add to collection */ 128 spawnNewBlock: function() { 129 var shapePos = _.random(app.shapes.length - 1); 130 this.collection.add([app.shapes[shapePos]]); 131 }, 132 133 /* Dispatch key commands */ 134 keyAction: function(e) { 135 var code = e.keyCode || e.which; 136 if (!this.paused) { 137 if (code == 37) { 138 this.moveLeft(); 139 } else if (code == 39) { 140 this.moveRight(); 141 } else if (code == 40) { 142 this.moveDown(); 143 } else if (code == 38) { 144 this.rotate(); 145 } 146 } 147 if (code == 80) { 148 this.pause(); 149 } 150 if (code == 78) { 151 this.newGame(); 152 } 153 if (code == 77) { 154 this.mute(); 155 } 156 }, 157 158 /* Pause or unpause game */ 159 pause: function() { 160 this.paused = !this.paused; 161 if (this.paused) { 162 $('#pause').html('Unpause (p)'); 163 $('#message').html('Paused.') 164 clearInterval(app.interval); 165 } else { 166 $('#pause').html('Pause (p)'); 167 $('#message').html(''); 168 this.start(); 169 } 170 }, 171 172 /* Toggle mute */ 173 mute: function() { 174 this.muted = !this.muted; 175 if (this.muted) { 176 $('#mute').html('Unmute (m)'); 177 } else { 178 $('#mute').html('Mute (m)'); 179 } 180 }, 181 182 /* Add a landed block to the underlying grid */ 183 updateGrid: function() { 184 this.collection.each(function(model) { 185 gridView.updateGrid(model) 186 gridView.logGrid(); 187 }, this); 188 }, 189 190 checkGameOver: function(block) { 191 var blockY = block.get('y'); 192 if (blockY <= 0) { 193 this.gameOver(); 194 } 195 }, 196 197 gameOver: function() { 198 gridView.drawGrid(); 199 this.playAudio('gameOver'); 200 clearInterval(app.interval); 201 $('#message').html('GAME OVER!') 202 }, 203 204 playAudio: function(key) { 205 if (!this.muted) { 206 var player = new Audio(); 207 player.src = jsfxr(app.sounds[key]); 208 player.play(); 209 } 210 }, 211 212 newGame: function() { 213 this.start(); 214 this.playAudio('newGame'); 215 $('#message').html(''); 216 this.collection.reset(); 217 gridView.clearGrid(); 218 gridView.drawGrid(); 219 gridView.logGrid(); 220 this.spawnNewBlock(); 221 }, 222 223 /* Check to see if any rows are full */ 224 checkCompleteRows: function() { 225 var completeRows = []; 226 for (var y = 0; y < app.grid.length; y++) { 227 var row = app.grid[y]; 228 var complete = true; 229 for (var x = 0; x < row.length; x++) { 230 if (row[x] != 1) { 231 complete = false; 232 } 233 } 234 if (complete) { 235 completeRows.push(y); 236 } 237 } 238 if (completeRows.length > 0) { 239 this.clearCompleteRows(completeRows); 240 } else { 241 this.playAudio('land'); 242 } 243 }, 244 245 /* Clear any complete rows from the grid and add a new clean row to the top */ 246 clearCompleteRows: function(completeRows) { 247 this.playAudio('completeRow'); 248 for (var i = 0; i < completeRows.length; i++) { 249 var rowIndex = completeRows[i]; 250 app.grid.splice(rowIndex, 1); 251 var row = []; 252 for (var x = 0; x < app.width; x++) { 253 row.push(0); 254 } 255 app.grid.unshift(row); 256 } 257 gridView.logGrid(); 258 }, 259 260 /* Move a block left on keyboard input */ 261 moveLeft: function() { 262 var self = this; 263 this.collection.each(function(model) { 264 var newX = model.get('x') - 1; 265 if (model.get('x') > 0 && self.shapeFits(model.get('shape'), newX, model.get('y'))) { 266 model.set('x', newX); 267 self.playAudio('bluhp'); 268 } 269 }); 270 }, 271 272 /* Move a block right on keyboard input */ 273 moveRight: function() { 274 var self = this; 275 this.collection.each(function(model) { 276 var newX = model.get('x') + 1; 277 if (model.get('x') + model.get('width') < app.width && self.shapeFits(model.get('shape'), newX, model.get('y'))) { 278 model.set('x', newX); 279 self.playAudio('bluhp'); 280 } 281 }); 282 }, 283 284 /* Move a block down on keyboard input */ 285 moveDown: function() { 286 var self = this; 287 this.collection.each(function(model) { 288 var newY = model.get('y') + 1; 289 if (model.get('y') + model.get('height') < app.height && self.shapeFits(model.get('shape'), model.get('x'), newY)) { 290 model.set('y', newY); 291 self.playAudio('bluhp'); 292 } 293 }); 294 }, 295 296 /* Automatically move a block down one step */ 297 descend: function() { 298 var self = this; 299 this.collection.each(function(model) { 300 if (model.get('y') + model.get('height') < app.height && self.shapeFits(model.get('shape'), model.get('x'), model.get('y') + 1)) { 301 model.set('y', model.get('y') + 1); 302 } else { 303 self.blockLanded(model); 304 } 305 }); 306 }, 307 308 /* Check if a given shape is within the bounds of the play area */ 309 shapeWithinBounds: function(shape, x, y) { 310 if (x < 0) { 311 return false; 312 } 313 if (x + shape[0].length > app.width) { 314 return false; 315 } 316 return true; 317 }, 318 319 /* Check if a shape fits in the play area and with the other blocks */ 320 rotatedShapeFits: function(shape, x, y) { 321 return this.shapeFits(shape, x, y) && this.shapeWithinBounds(shape, x, y); 322 }, 323 324 /* Rotate a block */ 325 rotate: function() { 326 var self = this; 327 this.collection.each(function(model) { 328 // Method 1: (from http://stackoverflow.com/a/34164786) 329 // Transpose the shape matrix (from http://stackoverflow.com/a/17428779): 330 var transposed = _.zip.apply(_, model.get('shape')); 331 var reversed = transposed.reverse(); 332 if (self.rotatedShapeFits(reversed, model.get('x'), model.get('y'))) { 333 model.set('shape', reversed); 334 var shape = model.get('shape'); 335 model.set('width', shape[0].length); 336 model.set('height', shape.length); 337 self.playAudio('bluhp'); 338 } 339 }); 340 }, 341 342 /* Check if a block collides with landed blocks */ 343 shapeFits: function(shape, shapeX, shapeY) { 344 for (var y = 0; y < shape.length; y++) { 345 var row = shape[y]; 346 for (var x = 0; x < row.length; x++) { 347 if (row[x] == 1) { 348 var checkX = shapeX + x; 349 var checkY = shapeY + y; 350 if (app.grid[checkY][checkX] == 1) { 351 return false; 352 } 353 } 354 } 355 } 356 return true; 357 } 358}); 359 360app.ControlsView = Backbone.View.extend({ 361 362 el: $('#controls'), 363 364 events: { 365 'click #pause': 'pause', 366 'click #mute': 'mute', 367 }, 368 369 pause: function() { 370 app.events.trigger('pause'); 371 } 372 373//コードが長いので割愛します

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

y_waiwai

2019/09/03 05:52

どれのはなしですか? その行を提示しましょう
programing_you

2019/09/03 05:54

申し訳ありません! 6,7,10,20行目など、複数あります!
y_waiwai

2019/09/03 05:57

だからそれを提示しようよw
m.ts10806

2019/09/03 06:11

提示コードに行数があるわけではないのでコメントなどで // ←ここ とするのが伝わりやすい表現方法ですね
guest

回答2

0

JavaScript内(文字列やコメントは除く)でコロンが使われるパターンは、以下の3つです。

  • オブジェクトリテラルのキー({ foo: 'bar' }のコロン)
  • 三項演算子の一部(a ? b : cのコロン)
  • ラベル(switch文内のcase 1:などのコロン)

投稿2019/09/03 05:52

編集2019/09/03 05:55
maisumakun

総合スコア145184

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

yambejp

2019/09/03 05:53

ラベルはswitch使えば多用しませんか?
maisumakun

2019/09/03 05:54

そういえばそうだった(breakで飛ぶラベルの方を想定していました)
yambejp

2019/09/03 06:10

二重ループからの離脱とかはたしかにめったにないですよね (知識ではもってても使ったケースはないです)
guest

0

たとえばオブジェクトのキーと値を紐付ける書き方のことでしょうか?

javascript

1var obj={a:1,b:2}; 2console.log(obj.a) //1

投稿2019/09/03 05:52

yambejp

総合スコア114839

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問