Visit Mozilla.org

Sharp variables in JavaScript

From MDC

A sharp variable is a syntax in object initializers that allows serialization of objects that have cyclic references or multiple references to the same object.

Sharp variables are a non-standard syntax for creating or serializing cyclic data graphs supported only by Mozilla's SpiderMonkey JS engine. Do not use them in non-Mozilla code, and even in Mozilla code consider the cleaner style of either multiple statements or a let-expression for constructing cyclic values.

Contents

[edit] Examples

[edit] Multiple References

var a = {};
var b = {};
b.a1 = a;
b.a2 = a;
print(b.toSource());

This outputs:

({a1:#1={}, a2:#1#})

[edit] Cyclic References

var a = {};
var b = {a0:123};
b.a1 = a;
b.a2 = a;
b.a3 = 'test';
print(b.toSource());

This outputs:

({a0:123, a1:#1={}, a2:#1#, a3:"test"})

[edit] See Also

JavaScript, Variables, Literals