【问题标题】:What does ".then(() => Storage.deployed())" actually mean in Javascript? [duplicate]“.then(() => Storage.deployed())”在 Javascript 中的实际含义是什么? [复制]
【发布时间】:2019-03-03 12:11:52
【问题描述】:

我正在学习 Solidity,部署脚本是这样说的,

var Storage = artifacts.require("./Storage.sol");
var InfoManager = artifacts.require("./InfoManager.sol");

    module.exports = function(deployer) {

        // Deploy the Storage contract
        deployer.deploy(Storage)
            // Wait until the storage contract is deployed
            .then(() => Storage.deployed())
            // Deploy the InfoManager contract, while passing the address of the
            // Storage contract
            .then(() => deployer.deploy(InfoManager, Storage.address));
    }

而且我在 Google 上似乎无法找到右箭头字符“=>”。

【问题讨论】:

  • 我们在 javascript 胖箭头函数中调用 this,与匿名操作中的回调不同,您可以在父 (this) 的上下文中访问

标签: javascript solidity truffle


【解决方案1】:

() => 是 Javascript 中的箭头函数。

定义

箭头函数表达式是一种语法紧凑的替代方案 一个正则函数表达式,虽然没有自己的绑定 this、arguments、super 或 new.target 关键字。箭头功能 表达式不适合作为方法,它们不能用作 构造函数。

Read more about arrow functions

.then()

定义:

then() 方法返回一个 Promise。它最多需要两个参数: Promise 成功和失败情况的回调函数。

Read more about Promise.prototype.then()

发生的情况是,当 deployer.deploy(Storage) 承诺已解决时,您将执行函数 storage.deployed() 作为回调函数。

【讨论】:

    【解决方案2】:

    右箭头(=>)表示一个箭头函数,与你可能更熟悉的匿名函数(function() { ...body })非常相似。

    它们的行为非常相似,除了匿名函数会改变this 指向的内容,而箭头函数不会。在此示例中,使用哪个并不重要,但箭头函数通常受到青睐,因为它允许更简单的代码。

    【讨论】:

      猜你喜欢
      • 2019-05-03
      • 2023-04-01
      • 1970-01-01
      • 2021-10-07
      • 2013-01-04
      • 2010-12-06
      • 2015-02-21
      • 2010-11-05
      • 2013-06-13
      相关资源
      最近更新 更多