Skip to content

Variables and Paths

Johan Hernandez edited this page Nov 23, 2011 · 4 revisions

Working with Variables

fire.js supports storing and recovering data for later use providing expressions to set and get variables.

Example:

{
	"name": "Examples.Variables",
	"json": {
		"@set(name)": "Chuck Norris",
		"@get(name)": null
	}
}

Download examples/Examples.SimpleNamesVariables.fjson and execute to see the results:

$ firejs Examples.SimpleNamesVariables.fjson
"Chuck Norris"

Both @set and @get will take a hint with the name of the variable. While the input of @set is the value of the variable to be set, @get totally ignores the input and just returns the value of the variable.

Note: Names cannot contain spaces or punctuation marks like _ $ or .

Scopes

Variables are available for inner scopes in the JSON document, this example uses @set in the first level and @get in the second level of the expression-block:

{
	"name": "Examples.ScopedVariables",
	"json": {
		"@set(fName)": "Chuck",
		"@set(lName)": "Norris",
		"@return": {
			"firstName": { 
				"@get(fName)": null 
				},
			"lastName": {
				"@get(lName)": null
			}
		}
	}
}

Download examples/Examples.ScopedVariables.fjson and execute to see the results:

$ firejs Examples.ScopedVariables.fjson
{"firstName":"Chuck","lastName":"Norris"}

Variables follows some simple rules about scopes:

  • Values set to variables from outer scopes are available to inner scopes.
  • Values set from inner scopes are not available to outer scopes unless the variable was previously declared in an outer scope.

The following example shows how to replace variables values from inner scopes.

{
	"name": "Examples.OuterVariables",
	"json": {
			"@set(person)": null,
			"@return": {
				"@set(person)": {
					"firstName": "Chuch",
					"lastName": "Norris"
				},
			"@get(person)": null
		}
	}
}

Download examples/Examples.OuterVariables.fjson and execute to see the results:

$ firejs Examples.OuterVariables.fjson 
{"firstName":"Chuch","lastName":"Norris"}

There is a variant of @set expression that will always declare a variable no matter if there is already a variable reachable from the current scope, @scopeSet.

More about how @get and @set behaves with undefined values

@set does not alter the current value of the block, this is known as 'bypass'.

Note: Check the reference to know which built-in expressions bypass the results.

To illustrate this behavior the following fire snippet returns undefined since @set will bypass the current result of the expression-block and the initial result of any expression-block is always undefined:

{
    "@set(x)": "This variable is never used"
}

The result is undefined.

@get on the other hand will return undefined if the variable doesn't not exists, was not declared in the current expression-block or is out of scope of the current expression block.

The following example snippet will try to use an undeclared variable. This will not cause a crash but it will return undefined

{
    "@get(someUndeclaredVariable)": null
}

Working with Paths

We previously said that @set will set a variable name with the given input but the thing is that @get and @set can get, set and even create variables, members and sub-members. This is because the hint of those expressions acts as a path

Paths uses dot notation for members.

The following program will extract the name from the superhero object using dot notation:

{
	"name": "Examples.DotNotationPaths",
	"json": {
		"@set(superhero)": {
			"name": "Chuck Norris",
			"age": 460
		},
		"@get(superhero.name)": null
	}
}

Download examples/Examples.Examples.DotNotationPaths.fjson and execute to see the results:

$ firejs Examples.DotNotationPaths.fjson
"Chuck Norris"

You can use as many nested members as necessary, if at some level of the path one member is undefined or null it will return undefined without crashing.

Notice how none of the examples uses indexes as part of the path, this is because paths do not support index references, you should use @index or @each instead.

Thanks for Reading. Please take your time to read other tutorials.

Contributors

If you find any error in this tutorial(misspelling errors, syntax errors, etc) please send me an email with your corrections, I'll be more than happy to add you in this list.