- Allow to modify behavior of the function in reusable and declarative form.
- In common words, it is a middlewares(in term of apis) where the validation of the inputs, add more functionality, etc is done.
- It can be used to add the functionlity/validation to multiple group of functions or single, before or after the function execution.
- e.g onlyOwner modifier
contract Modifiers {
address owner;
uint num;
constructor(){
owner = msg.sender; // sets the address of contract deployer / owner
}
modifier onlyOwner(){
require(msg.sender == owner, "Access denied");
_; // similar to next() in express
}
// only owner can perform the below func
function add(uint _num) public onlyOwner {
num += _num
}
function subtract(uint _num) public onlyOwner {
num -= _num
}
}