Javascript 访问属性“toString”的权限被拒绝
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13864786/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Permission denied to access property 'toString'
提问by Anders
I'm trying to find a generic way of getting the name of Constructors. My goal is to create a Convention over configuration framework for KnockoutJS
我试图找到一种获取构造函数名称的通用方法。我的目标是为 KnockoutJS 创建一个约定优于配置框架
My idea is to iterate over all objects in the window and when I find the contructor i'm looking for then I can use the index to get the name of the contructor
我的想法是遍历窗口中的所有对象,当我找到我正在寻找的构造函数时,我可以使用索引来获取构造函数的名称
The code sofar
到目前为止的代码
(function() {
constructors = {};
window.findConstructorName = function(instance) {
var constructor = instance.constructor;
var name = constructors[constructor];
if(name !== undefined) {
return name;
}
var traversed = [];
var nestedFind = function(root) {
if(typeof root == "function" || traversed[root]) {
return
}
traversed[root] = true;
for(var index in root) {
if(root[index] == constructor) {
return index;
}
var found = nestedFind(root[index]);
if(found !== undefined) {
return found;
}
}
}
name = nestedFind(window);
constructors[constructor] = name;
return name;
}
})();
var MyApp = {};
MyApp.Foo = function() {
};
var instance = new MyApp.Foo();
console.log(findConstructorName(instance));
The problem is that I get a Permission denied to access property 'toString'Exception, and i cant even try catch so see which object is causing the problem
问题是我得到了一个Permission denied to access property 'toString'异常,我什至不能尝试 catch 所以看看是哪个对象导致了问题
Fiddle http://jsfiddle.net/4ZwaV/
Final version in this fiddlehttp://jsfiddle.net/2Uvd5/8/
这个小提琴的最终版本http://jsfiddle.net/2Uvd5/8/
Check here for the embryo of my Convention over configuration pluginhttps://github.com/AndersMalmgren/Knockout.BindingConventions
在此处查看我的约定优于配置插件的胚胎https://github.com/AndersMalmgren/Knockout.BindingConventions
采纳答案by Martijn Hols
- Edit2:
- 编辑2:
This solves everything except for one thing: var MyApp = {};doesn'tadd it to the window-object. Changing that to window.MyApp = {};makes it completely working (even within an IFrame).
这解决了所有事情,除了一件事:var MyApp = {};不将它添加到window-object. 将其更改为window.MyApp = {};使其完全工作(即使在 IFrame 中)。
- Edit1:
- 编辑1:
Adding to the array by setting the key name requires the key name to be a string so Javascript will automatically call. toString()on your suggested keyname which will fail for certain objects. Instead use .push() to add elements of any type to an array and then .indexOf()to check if it already exists.
通过设置键名添加到数组需要键名是一个字符串,这样Javascript 会自动调用。toString()在您建议的键名上,这对于某些对象会失败。而是使用 .push() 将任何类型的元素添加到数组中,然后.indexOf()检查它是否已经存在。
Do note that the jsFiddle still breaks because of being placed in an iframe. Opening it in a new tabsolves that.
请注意,jsFiddle 由于被放置在iframe. 在新选项卡中打开它可以解决这个问题。
My previous answer(which proved to be invalid when I tried to verify it in your jsFiddle):
我之前的答案(当我试图在你的 jsFiddle 中验证它时被证明是无效的):
You need to check if the constructor is an exact Object. If it is then calling .toString() on it will cause a security exception which I found to be kinda hard to debug. Here's a function I use to get the type of an object in a var-dumper I use.
您需要检查构造函数是否是一个精确的对象。如果它然后调用 .toString() 将导致安全异常,我发现它有点难以调试。这是我用来在我使用的 var-dumper 中获取对象类型的函数。
function GetTypeOfObject(obj) {
if (obj.constructor === window.Object)
return '[object]';
else
return obj.constructor.toString();
}

