a <- function() self$x
e <- new.env()
e$x <- 1
# Register the method `aa` for environment `e` with `self_name = "self"`
register_method(e, aa = a, self_name = "self", class_name = "test")
# There is an environment `..method_env..` in the environment `e`
names(e)
# The container is empty (except `self`)
names(e$..method_env..)
# `self` is a reference to `e`
identical(e, e$..method_env..$self)
# The method `aa` will be evaluated in the container
identical(environment(e$aa), e$..method_env..)
# Therefore, `self$x` is a reference to variable `x` of the environment `e`
e$aa()
Run the code above in your browser using DataLab