x:y 在 JavaScript 中的语法是什么意思?

What does the syntax x:y mean in JavaScript?

我正在学习区块链课程,其中包含以下代码。 “index:this.chain.length+1”是什么意思? index是对象newBlock中的一个变量吗?还是键值对?如果它是一个变量,我们为什么不简单地使用index=this.chain.length+1呢?还有对象 newBlock 的类型是什么?

function Blockchain()
{
  this.chain=[];
  this.newTranscations=[];
}

Blockchain.prototype.createNeBlock = function(nonce,previousBlockHash,hash)
{
  const newBlock ={
    index:this.chain.length+1,
    timestamp:Date.now(),
    // all of the transactions in this block will be the transactions that waiting to be put in a block
    transactions:this.newTranscations,
    // nonce is hust a number giving proof of the transaction
    nonce:nonce,
    hash:hash,
    previousBlockHash: previousBlockHash
  }
  // As we move all the pending transactions to the new block, we clear this array
  this.newTranscations=[];
  this.chain.push(newBlock);
  return newBlock; 
}
var Box = {
"playdoh":{"playdoh":["none", "some", "none", "none", "some"]}
};

一盒一盒的橡皮泥,你正在学习Objects/Arrays/Maps。

要调用上面的内容,应该是

console.log(Box["playdoh"]["playdoh"][0]);
= none

console.log(Box["playdoh"]["playdoh"][4]);
= some

console.log(Box["playdoh"]["playdoh"][5]);
= null (undefined)

相同
console.log(Box.playdoh.playdoh[0]);
= none

console.log(Box.playdoh.playdoh[4]);
= some

console.log(Box.playdoh.playdoh[5]);
= null (undefined)

它是在 javascript 中初始化名为 newBlock 的对象的几种方法之一。看看这个documentation on MDN

本例中的index 属性为number类型,设置为等于chain[].length + 1