asdg>> technotes>> undocumented #strict pragma

Version: Flash MX
Date Added: December 2, 2002

The #strict compiler pragma has been available but undocumented since Flash 5. It is volatile and should not be used, despite the fact that it was briefly made public during the beta of Flash Player 6.0.61.0. Macromedia rescinded support for #strict in Flash Player 6 due to various problems, the worst of which was crashing Internet Explorer on Windows.

For the curious: the #strict pragma forces the Flash Player to conform more closely to the ECMA-262 specification, upon which ActionScript is based. When the #strict pragma appears on a line of its own on frame 1 of a Flash document, the ActionScript interpreter's behavior changes in the following ways:

  • Conversion of the value undefined to a number yields NaN. (In non-strict mode, conversion of undefined to a number yields 0.)
  • Conversion of the value undefined to a string yields "undefined". (In non-strict mode, conversion of undefined to a string yields the empty string ("").)
  • Identifiers, keywords, and all language tokens are case sensitive. (In non-strict mode, only keywords are case sensitive.)
  • Conversion of a non-empty string to a Boolean yields true; conversion of an empty string to a Boolean yields false. (In non-strict mode, conversion of a string to a Boolean yields true only if the string can be converted to a valid nonzero number.)

Note that #strict is a Player-wide setting that has an effect only when used on frame 1 of a document. Adding #strict to a single movie forces strict mode on all other movies loaded into the Player. There is no way to enable strict mode for a single .swf file only. Strict mode cannot be disabled once it is enabled.

The following example checks if a string is empty by converting it to a Boolean. In strict mode, if a string is empty, it will convert to false, otherwise it will convert to true.

#strict
var name = "Colin";
if (!name) {
  trace("No name specified");
} else {
  trace("Hello, " + name);
}

The following example creates a new variable named date that stores a Date instance. The date variable does not overwrite the Date class constructor because the two identifiers are considered unique according to the strict-mode rules of case sensitivity.

#strict
var date = new Date();