官方解释:

这个叫做fallback function,当有人 1. 只发送以太币给合约而不带任何输入数据;2. 调用smart contract时调起了一个不存在的方法。会触发执行这个方法。

What is the deal with “function () { ... }” inside Solidity contracts? How can a function not have a name?

This function is called “fallback function” and it is called when someone just sent Ether to the contract without providing any data or if someone messed up the types so that they tried to call a function that does not exist.

The default behaviour (if no fallback function is explicitly given) in these situations is to just accept the call and do nothing. This is desireable in many cases, but should only be used if there is a way to pull out Ether from a contract.

If the contract is not meant to receive Ether with simple transfers, you should implement the fallback function as

function() { throw; }

this will cause all transactions to this contract that do not call an existing function to be reverted, so that all Ether is sent back.

Another use of the fallback function is to e.g. register that your contract received ether by using an event.

Attention: If you implement the fallback function take care that it uses as little gas as possible, because send() will only supply a limited amount.

如果不定义这个方法,所有被误转到这个合约上的以太币将被拒绝。

Solidity 没名字的function(){...}作用的更多相关文章

  1. 【JS】 +function(){} 作用

    原文地址:https://www.jianshu.com/p/a2666014a280 瞎扯 在JS中,经常会遇到下面这种 代码, 到底 在 function 前面加一个 一元操作符, 有什么作用呢? ...

  2. JavaScript原型链以及Object,Function之间的关系

    JavaScript里任何东西都是对象,任何一个对象内部都有另一个对象叫__proto__,即原型,它可以包含任何东西让对象继承.当然__proto__本身也是一个对象,它自己也有自己的__proto ...

  3. java代码之美(15)---Java8 Function、Consumer、Supplier

    Java8 Function.Consumer.Supplier 有关JDK8新特性之前写了三篇博客: 1.java代码之美(1)---Java8 Lambda 2.java代码之美(2)---Jav ...

  4. java代码(15) ---java8 Function 、Consumer 、Supplier

    Java8 Function.Consumer.Supplier 有关JDK8新特性之前还有三篇博客: 1,java代码(1)---Java8 Lambda 2,java代码(2)---Java8 S ...

  5. c++11 function bind 测试。

    实验小结 1)function 是一个模板类.有函数指针成员.可以看作安全型函数指针. template<typename _Res, typename... _ArgTypes> cla ...

  6. JavaScript中的Function类型浅析

    1. Function类型是js中引用类型之一,每个函数实际上都是Function类型的实例对象,具有自己的属性和方法.正因为函数式对象,所以函数名实际上也是一个指向函数对象的指针. 2. 常用的函数 ...

  7. Python3 与 C# 基础语法对比(Function专栏)

      Code:https://github.com/lotapp/BaseCode 多图旧版:https://www.cnblogs.com/dunitian/p/9186561.html 在线编程: ...

  8. C++ 名字空间namespace的使用

    A namespace is a scope.C++ provides namespaces to prevent name conflicts.A namespace is a mechanism ...

  9. 【转载】resolv.conf中search作用

    原文地址:http://www.oliver.ren/linux/387.html reslov.conf中的search主要是用来补全hostname的,有时候域名太长,可以做一个短域名做主机名字, ...

随机推荐

  1. 神奇的TextField(1)

    先看一大段测试代码,每个小方法的注释行是输出结果. private var text_content:TextField; private function textFieldDemo():void{ ...

  2. Jar包进行反编译,修改后重新打包

    在学习和开发JAVA项目中,我们经常会用到第三方提供的一些jar.使用这些第三方工具包,可以提高我们开发的效率,缩短开发的时间.有的第三方工具,提供具体的使用说明和源代码,有时有的却不提供源代码,使用 ...

  3. 洛谷P1119 灾后重建

    传送门 题目大意:点被破坏,t[i]为第i个点修好的时间,且t[1]<t[2]<t[3].. 若干询问,按时间排序,询问第t时刻,u,v的最短路径长度. 题解:floyed 根据时间加入点 ...

  4. Google搜索被屏蔽,如何使用Google搜索

    我们在国内使用搜索引擎最多的是Google和Baidu啦,在引擎上找一些我们需要的知识,最近好像www.google.cn已经无法访问了,并且香港的链接www.google.com.hk也无法访问了, ...

  5. CENTOS7安装DOCKER步骤以及安装阿里镜像加速后无法正常启动服务的问题2018年1月

    本文时间2017年12月7日,比较新,大家可以直接参考.有问题直接评论 我根据菜鸟教程通过yum install docker安装了docker,由于测试发现奇慢无比,所以就安装了阿里云的加速,随后也 ...

  6. Could not find class 'org.ksoap2.serialization.SoapObject

    Could not find class 'org.ksoap2.serialization.SoapObject工程编译没问题,一在模拟器运行就报错! 这是由于ADT版本过高引发的问题,解决办法: ...

  7. jq添加和移除事件的方法,prop和attr

    会在写条件判断的时候遇到,今天在判断没有剩余产品的时候,移除事件.当有产品的时候添加事件: 移除onClick事件: $("a").removeAttr("onclick ...

  8. 删除SVN被锁定文件

    svn的working copylocked这种情况大多是因为上次svn命令执行失败且被锁定了. 如果cleanup没有效果的话只好手动删除锁定文件. cd 到svn项目目录下,然后执行命令:del ...

  9. 织梦 dede 笔记

    将项目转移到另一服务器 方法: https://www.genban.org/news/dedecms-13096.html 在实际中,我走的是第二种方法 方法一: 1  后台>系统>备份 ...

  10. show-overflow-tooltip 宽度设置

    设置样式:不能放在scoped中 <style lang="scss"> .el-tooltip__popper{max-width:80%}</style> ...