Components.utils.cloneInto

跳转到:

这篇翻译不完整。请帮忙从英语翻译这篇文章

该函数为定义在某一作用域中的对象提供了一个安全的、将其结构化克隆到其字作用域中的方法,并返回对克隆对象的引用:

var clonedObject = cloneInto(myObject, targetWindow);

可以将克隆后的对象作为可扩展对象的动态属性引入到特定作用域中,以便运行在该作用域的脚本进行访问:

targetWindow.foo = clonedObject;

通过这种方式,运行在某一作用域中的代码可以和运行在另一作用域中的代码共享对某一对象的访问。

语法

Components.utils.cloneInto(obj, targetScope[, options]);

参数列表

obj : object
被克隆对象。
targetScope : object
对象克隆后的容器。
options : object
该参数为一可选参数。该参数为一拥有以下可选属性的对象:

返回值

对克隆后的对象的引用。

范例

This add-on script creates an object, clones it into the content window and makes it a property of the content window global:

// add-on script

var addonScriptObject = {"greeting" : "hello from add-on"};
contentWindow.addonScriptObject = cloneInto(addonScriptObject, contentWindow);

Scripts running in the page can now access the object:

// page script

button.addEventListener("click", function() {
  console.log(window.addonScriptObject.greeting);     // "hello from add-on"
}, false);

Of course, you don't have to assign the clone to the window itself: you can assign it to some other object in the target scope:

contentWindow.foo.addonScriptObject = cloneInto(addonScriptObject, contentWindow);

You can also pass it into a function defined in the page script. Suppose the page script defines a function like this:

// page script

function foo(greeting) {
  console.log("they said: " + greeting.message);
}

The add-on script can define an object, clone it, and pass it into this function:

// add-on script

var addonScriptObject = {"message" : "hello from add-on"};
contentWindow.foo(cloneInto(addonScriptObject, contentWindow));  // "they said: hello from add-on"

Cloning objects that have functions

If the object to be cloned contains functions, you must pass the {cloneFunctions:true} flag or you'll get an error. If you do pass this flag, then functions in the object are cloned using the same mechanism as that used in Components.utils.exportFunction:

// add-on script

var addonScriptObject = {
  greetme: function() {
    alert("hello from add-on");
  }
};

contentWindow.addonScriptObject = cloneInto(addonScriptObject,
                                           contentWindow,
                                           {cloneFunctions: true});
// page script

var test = document.getElementById("test");

test.addEventListener("click", function() {
  window.addonScriptObject.greetme();
}, false);

Cloning objects that contain DOM elements

By default, if the object you clone contains objects that are reflected from C++, such as DOM elements, the cloning operation will fail with an error. If you pass the {wrapReflectors:true} flag, then the object you clone is allowed to contain these objects:

// add-on script

var addonScriptObject = {
  body: contentWindow.document.body
};

contentWindow.addonScriptObject = cloneInto(addonScriptObject,
                                           contentWindow,
                                           {wrapReflectors: true});
// page script

var test = document.getElementById("test");

test.addEventListener("click", function() {
  console.log(window.addonScriptObject.body.innerHTML);
}, false);

Access to these objects in the target scope is subject to the normal security checks.

文档标签和贡献者

此页面的贡献者: xdyushenli
最后编辑者: xdyushenli,