【发布时间】:2021-01-07 04:57:08
【问题描述】:
我正在学习有关区块链的课程,其中包含以下代码。 “ index:this.chain.length+1 ”是什么意思?索引是对象 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;
}
【问题讨论】:
-
在对象字面量中,冒号将属性名称与其将包含的值分开
-
如果您正在学习的课程中没有说明这一点,我建议您更改课程。
标签: javascript syntax