Components.utils.createObjectIn

Table of contents

    1. 1.1. Syntax
    2. 1.2. Parameters
    3. 1.3. Return value
    4. 1.4. Example
  1. 2. See also
Table of contents
    1. 1.1. Syntax
    2. 1.2. Parameters
    3. 1.3. Return value
    4. 1.4. Example
  1. 2. See also

Introduced in Gecko 8.0

(Firefox 8.0 / Thunderbird 8.0 / SeaMonkey 2.5)

This method creates a new JavaScript object in the scope of the specified object's compartment.

Syntax

var newObject = Components.utils.createObjectIn(obj);

Parameters

obj
An object indicating the compartment in which the new object should be created; the new object will be created in the scope of this object's compartment.

Return value

A new object in the specified scope.

Example

To create a new object in the scope of a specified DOM window, you can simply do:

function genPropDesc(value) {
  return {
    enumerable: true, configurable: true, writable: true, value: value
  };
}

var myObject = Components.utils.createObjectIn(myWindow);

var propList = {
  name: genPropDesc("name"),
  date: genPropDesc("date"),
  id: genPropDesc("id"),
  func: genPropDesc(function() { alert("Called func!"); })
};

Object.defineProperties(myObject, propList);
Components.utils.makeObjectPropsNormal(myObject);

This sets up the new object in the scope of the object myWindow, then adds properties by calling Object.defineProperties(), then normalizes them by calling Components.utils.makeObjectPropsNormal().

Tags (7)

Edit tags

Attachments (0)

 

Attach file