Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.
From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).
From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.
Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.
A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.
Unstable
Functions for working with type detection.
Globals
Functions
isUndefined(value)
Returns true
if value
is undefined
, false
otherwise.
let { isUndefined } = require('sdk/lang/type');
var foo; isUndefined(foo); // true isUndefined(0); // false
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is undefined
.
isNull(value)
Returns true
if value
is null
, false
otherwise.
let { isNull } = require('sdk/lang/type');
isNull(null); // true
isNull(false); // false
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is null
.
isString(value)
Returns true
if value
is a String
, false
otherwise. Uses typeof
operator to check type, and will only properly detect string primitives: for example, a string created with new String()
will always return false.
let { isString } = require('sdk/lang/type');
isString('my string'); // true isString(100); // false isString('100'); // true
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is a String
.
isNumber(value)
Returns true
if value
is a Number
, false
otherwise. Uses typeof
operator to check type, and will only properly detect number primitives: for example, a number created with new Number()
will always return false.
let { isNumber } = require('sdk/lang/type');
isNumber(3.1415); // true isNumber(100); // true isNumber('100'); // false
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is a Number
.
isRegExp(value)
Returns true
if value
is a RegExp
, false
otherwise.
let { isRegExp } = require('sdk/lang/type');
isRegExp(/[^\.]*\.js$/); // true isRegExp(new RegExp('substring')); // true isRegExp(1000); // false
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is a RegExp
.
isDate(value)
Returns true
if value
is a Date
, false
otherwise.
let { isDate } = require('sdk/lang/type');
isDate(new Date()); // true isDate('3/1/2013'); // false
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is a Date
.
isFunction(value)
Returns true
if value
is a Function
, false
otherwise.
let { isFunction } = require('sdk/lang/type');
let fn = function () {}; isFunction(fn); // true; isFunction(otherFn); // true; isFunction(function () {}); // true;
function otherFn () {}
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is a Function
.
isObject(value)
Returns true
if value
is an Object
and not null, false
otherwise.
let { isObject } = require('sdk/lang/type');
isObject({}); // true isObject(new Class()); // true isObject(null); // false isObject(5); // false
function Class () {}
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is an Object
.
isArray(value)
Returns true
if value
is an Array
, false
otherwise. Uses native Array.isArray
.
let { isArray } = require('sdk/lang/type');
isArray([]); // true isArray({}); // false
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is an Array
.
isArguments(value)
Returns true
if value
is an array-like arguments
object, false
otherwise.
let { isArguments } = require('sdk/lang/type');
function run () { isArguments(arguments); // true isArguments([]); // false isArguments(Array.slice(arguments)); // false } run(1, 2, 3);
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is an arguments
object.
isPrimitive(value)
Returns true
if value
is a primitive value: that is, any of null
, undefined
, number
, boolean
, or string
. Returns false
if value
is not a primitive value.
let { isPrimitive } = require('sdk/lang/type');
isPrimitive(3); // true isPrimitive('foo'); // true isPrimitive({}); // false
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is a primitive.
isFlat(value)
Returns true
if value
is a direct descendant of Object.prototype
or null
. Similar to jQuery's isPlainObject
.
let { isFlat } = require('sdk/lang/type');
isFlat({}); // true isFlat(new Type()); // false
function Type () {}
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is a direct descendant of Object.prototype
or null
.
isEmpty(value)
Returns true
if value
is an Object
with no properties and false
otherwise.
let { isEmpty } = require('sdk/lang/type');
isEmpty({}); // true isEmpty({ init: false }); // false
Parameters
value : object
The variable to check.
Returns
boolean : Boolean indicating if value
is an Object
with no properties.
isJSON(value)
Returns true
if value
is a string, number, boolean, null, array of JSON-serializable values, or an object whose property values are themselves JSON-serializable. Returns false
otherwise.
let { isJSON } = require('sdk/lang/type');
isJSON({ value: 42 }); // true isJSON({ fn: function () {} ); // false
Parameters
value : mixed
The variable to check.
Returns
boolean : Boolean indicating if value
is an Array
/flat Object
containing only atomic values and other flat objects.
instanceOf(value, Type)
Returns true
if value
is an instance of a given Type
. This is similar to the instanceof
operator. The difference is that the Type
constructor can be from a scope that has a different top level object: for example, it could be from a different iframe, module or sandbox.
let { instanceOf } = require('sdk/lang/type');
instanceOf(new Class(), Class); // true function Class() {}
Parameters
value : object
The variable to check.
Type : object
The constructor to compare to value
Returns
boolean : Boolean indicating if value
is an instance of Type
.
source(value, indent, limit)
Returns the textual representation of value
, containing property descriptors and types of properties contained within the object.
let { source } = require('sdk/lang/type');
var obj = { name: undefined, twitter: '@horse_js', tweets: [ { id: 100, text: 'What happens to you if you break the monad laws?' }, { id: 101, text: 'JAVASCRIPT DUBSTEP GENERATOR' } ] };
console.log(source(obj)); // Prints the below /* { // [object Object] // writable configurable enumerable name: undefined, // writable configurable enumerable twitter: "@horse_js", // writable configurable enumerable tweets: [ { // [object Object] // writable configurable enumerable id: 100, // writable configurable enumerable text: "What happens to you if you break the monad laws?", "__proto__": { // [object Object]
} }, { // [object Object] // writable configurable enumerable id: 101, // writable configurable enumerable text: "JAVASCRIPT DUBSTEP GENERATOR", "__proto__": { // [object Object]
} } ], "__proto__": { // [object Object]
} } */
Parameters
value : mixed
The source object to create a textual representation of.
indent : string
Optional. String
to be used as indentation in output. 4 spaces by default.
limit : number
Optional. Number of properties to display per object.
Returns
string : The textual representation of value
.