A namespace is a collection of unique names and corresponding values. You can store references to namespaces in variables. Namespace names are global, because JMP has only one namespace map. Namespace references are variables like any other variable that references an object, so they must be unique within their scope or namespace. The members of a namespace are referenced with the : scoping operator, such as my_namespace:x to refer to the object that is named x within the namespace called my_namespace. See User-Defined Namespace Functions for details about creating and managing your own namespaces. Namespaces are especially useful for avoiding name collisions between different scripts.
Creates a new namespace called nsname (a string expression) and returns a reference to the namespace. All arguments are optional.
Nsname is the name of the namespace in the internal global list of namespace names. Nsname can be used as the prefix in a scoped variable. The function returns a reference to the namespace, and can also be used as the prefix in a scoped variable reference. If nsname is absent, the namespace is anonymous and is given a unique name created by JMP. Show Namespace() shows all namespaces and their names, whether assigned or anonymous.
Important: If you already have a namespace named nsname, it is replaced. This behavior means that while you are developing your script, you can make your changes and re-run the script without having to clear or delete your namespace. To avoid unintentional replacement, you can either use anonymous namespaces, or test to see whether a particular namespace already exists:
|
注意:Namespace() returns a reference to a namespace that already exists. It does not create a new namespace.
As Scoped() is the function form of a scoped reference. The function returns a reference to the specified variable in the specified scope.
Shows the contents of all namespaces contained in the list of global namespaces. Namespaces are not visible unless a reference is made to one, using either the New Namespace or Namespace functions.
Note that these messages, as with all message, must be sent to a scriptable object. A namespace name is not a defined scriptable object and cannot be used in a Send operation. However, you can use the name of a namespace in variable references. For example, nsmane::var is equivalent to nsref::var.
Namespace Messages defines the messages that are supported by user-defined namespace references.
Returns 1 or 0, depending on whether var_name exists within the namespace.
|
|
Returns the unevaluated expression that var_name contains in this namespace.
|
|
Locks all variables in the namespace and prevents variables from being added or removed. <<Unlock unlocks all of the namespace’s variables.
|
|
The following are all equivalent references to a variable that is named b in the namespace that is named nsname that has a reference nsref:
•
|
use anonymous names created by the New Namespace function
|
There is also an option for the Include function (New Context) that creates a namespace that the included script runs in. This namespace is anonymous and it is independent from the parent script’s namespace. For example:
If both the parent and included scripts use the global namespace, add Names Default To Here to the New Context option to avoid name collisions. For example:
new_emp = New Namespace(
{name_string = "Hello, *NAME*!",
print_greeting = Function( {a},
complex:makec = Function( {a, b},
complex:addc = Function( {a, b}, a + b );
complex:subtractc = Function( {a, b}, a - b );
complex:multiplyc = Function( {a, b},
complex:charc = Function( {a},
cadd = complex:addc( c1, c2 ); // returns {8, 10}
csum = complex:subtractc( c1, c2 ); // returns {-2, -2}
cmul = complex:multiplyc( c1, c2 ); // returns {-9, 38}
cdiv = complex:dividec( c1, c2 ); // returns {14.6065573770492, 19.7049180327869}
Show( complex:charc( c1 ) ); // returns complex:char(c1) = "3+4i";