Solidity 中的修饰符和对值的访问

Modifiers in Solidity and access to values

作者摘录以下内容是什么意思?谁能提供一个很好的例子?

Inside a modifier, you can access all the values (variables and arguments) visible to the modified function. In this case, we can access the owner variable, which is declared within the contract. However, the inverse is not true: you cannot access any of the modifier’s variables inside the modified function.”

如果使用 "view" 范围声明函数,则该函数可以只读方式访问 "owner" 等全局变量。但是,修饰符无法写入 "owner"。

如果函数声明的范围是"pure",那么修饰符根本不能访问全局变量所有者。

归根结底,修饰符的限制只能与它们所修饰的函数相同或更严格。

此外,修饰符可以接受参数,因此您可以将发送到函数的参数传递给修饰符,如下所示。

contract token {
    uint owner;
    function foo(uint fooval) bar(fooval) view public{
    // this function is "view" so the modifier bar can only read owner at most
    // also, we can pass fooval on to the modifier

        uint infoo;
        infoo = owner;
    }

    modifier bar(uint barval) {
        uint hold;
        if (barval > 100) {
           // owner = 101; // not allowed, since foo is "view"
           hold = owner; // this is ok because foo is at least "view"
        }
        _;
    }
}