add
scope.add(context [,meta])
Creates a new scope and sets the current scope to be the parent.
const scope = new Scope( [
{ name: "Chris" },
{ name: "Justin" }
] ).add( { name: "Brian" } );
scope.get( "name" ); //-> "Brian"
Parameters
- context
{*}
:The context to add on top of the current scope.
- meta
{Meta}
:A meta option that can be used to configure special behaviors of this context.
Use
scope.add(context)
creates a new scope object. Values can be looked up in the parent scope
object by prefixing the key with "../"
. find can also be used to search in the parent’s context after the initial context is explored. For example:
const list = [ { name: "Justin" }, { name: "Brian" } ];
const justin = list[ 0 ];
const curScope = new Scope( list ).add( justin );
// use `get` to find a value in an explicit context
curScope.get( "name" ); //-> "Justin"
curScope.get( "../length" ); //-> 2
// use `find` to search for a value in any context
curScope.find( "name" ); //-> "Justin"
curScope.find( "length" ); //-> 2