- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想编写一个函数来列出对象及其原型(prototype)的所有可用属性(区分函数和其余属性),以了解如何操作该对象。
问题:无法访问某些!现有的!对象的属性,请参阅帖子末尾的示例代码,因为代码抛出如下类型错误:“TypeError:在未实现接口(interface) [...] 的对象上调用了 [...] getter”。
以下示例的确切错误代码:“TypeError:在未实现接口(interface) EventTarget 的对象上调用了‘ownerGlobal’ getter。”奇怪的是属性和 get&set 函数存在,请参阅:http://postimg.org/image/sh1okt4g7/ + console.log("属性存在:"+obj.hasOwnProperty(oneElem));
打印 true。
在研究这个问题时,我发现了这篇文章:can't read Element.prototype in firefox .
据我了解,所有类型错误都来自这样一个事实:原型(prototype)链中的不同 objectPrototypes 没有可访问的值,因为它们是原型(prototype)并且没有“真实”对象。 因此,所有抛出 TypeErrors 的属性都应该被丢弃,因为它们无法被使用。是这样吗?
另一方面,可以安全地假设所有剩余的属性都可以使用(调用函数、读取值),尽管它们是由原型(prototype)对象定义的?
源代码:
- 没那么有趣,但如果有人想测试一下,请随意
- 创建一个新的 Firefox 插件 (developer.mozilla.org/en-US/Add-ons/SDK/Tutorials/Getting_Started_(jpm)),复制并粘贴代码并运行插件
var mtabs = require("sdk/tabs");
//mtabs.open("http://lolvideoslol.tumblr.com/post/124427537261/lani-p-the-amount-of-pressure-put-on-the-last"); //open tab
mtabs.on('pageshow', newTabLoaded); //add listener
var { viewFor } = require("sdk/view/core"); //convert to low lvl to get document
function newTabLoaded(tabHigh) {
//get website doc
var document = viewFor(tabHigh.window).top.gBrowser.getBrowserForTab(viewFor(tabHigh)).contentDocument;
var nodeList = document.querySelectorAll("div[class^='video']"); //get interesting elements
analysePrototypeChain(nodeList[0]); //analyse the first element
//for (let i = 0, len = nodeList.length; i < len; i++) {
// analysePrototypeChain(nodeList[i]); //analyse the elements
//}
}
/**
* Prints the prototype chain and functions/other properties for the given object.
*
* @param {Object} obj to process.
*/
function analysePrototypeChain(obj) {
console.log("##############################################################");
console.log("######## checking prototypeChain for:", obj, "########");
let formatOptions = {"newLines": 0, "toString": 1};
printPrototypeChain(obj, [], formatOptions);
console.log();
let protos = getPrototypeChain(obj, []);
for (let i = 0, len = protos.length; i < len; i++) {
if (protos[i] == "[object EventTargetPrototype]") {
console.log("_"+i+":", protos[i]);
printProperties(protos[i]);
}
}
console.log("##############################################################");
}
/**
* Prints the complete prototype chain for the given object.
*
* @param {Object} obj to process.
* @param {Array} stringArr must be empty when first called.
* @param {Object} formatOptions to control the formatting of the output.
* let formatOptions = {"newLines": 0, "toString": 1};
*/
function printPrototypeChain(obj, stringArr, formatOptions) {
if (stringArr.length === 0) {
if (formatOptions.toString === 1) {
stringArr.push(obj+"");
} else {
stringArr.push(obj);
}
}
let proto = Object.getPrototypeOf(obj);
if (proto === null || proto === undefined) {
if (formatOptions.newLines === 1) {
stringArr.push("\n\n->");
} else {
stringArr.push("->");
}
if (formatOptions.toString === 1) {
stringArr.push(proto+"");
} else {
stringArr.push(proto);
}
console.log.apply(console, stringArr);
} else {
if (formatOptions.newLines === 1) {
stringArr.push("\n\n->");
} else {
stringArr.push("->");
}
if (formatOptions.toString === 1) {
stringArr.push(proto+"");
} else {
stringArr.push(proto);
}
return printPrototypeChain(proto, stringArr, formatOptions);
}
}
/**
* Returns all prototypes in the prototype chain for the given object.
*
* @param {Object} obj to process.
* @param {Array} stringArr must be empty when first called.
* @returns {Array} Array with the determined prototypes.
*/
function getPrototypeChain(obj, stringArr) {
if (stringArr.length === 0) {
stringArr.push(obj)
}
let proto = Object.getPrototypeOf(obj);
if (proto === null || proto === undefined) {
stringArr.push(proto);
return stringArr;
} else {
//if (proto == "[object EventTargetPrototype]") {
// Object.getOwnPropertyNames(Object.getPrototypeOf(obj)).forEach(function(val, idx, array) {
// console.log(val + ' -> ' + obj[val]);
// });
//}
stringArr.push(proto);
return getPrototypeChain(proto, stringArr);
}
}
/**
* Checks the given object for functions and other properties and prints them.
*
* @param {Object} obj to process.
*/
function printProperties(obj) {
if (obj === undefined || obj === null || obj === NaN) {
console.log(+"\n"+obj+" has no properties.");
return;
}
//for further usage
let functionList = [];
let remainingPropertiesList = [];
//output Strings
let functionString = "";
let remainingPropertiesString = "";
//checks all properties and fills functionList and remainingPropertiesList before invoking the next instruction
Object.getOwnPropertyNames(obj).forEach(checkElem);
console.log("Properties for:",obj+"");
let functionListLength = functionList.length;
if (functionListLength > 0) {
for (let i = 0, l = functionList.length-1; i < l; i++) {
functionString += functionList[i].type+" "+functionList[i].name+"(), ";
}
functionString += functionList[functionList.length-1].type+" "+functionList[functionList.length-1].name+"()";
}
let remainingPropertiesListLength = remainingPropertiesList.length;
if (remainingPropertiesListLength > 0) {
for (let i = 0, l = remainingPropertiesList.length-1; i < l; i++) {
remainingPropertiesString += "["+remainingPropertiesList[i].type+" "+remainingPropertiesList[i].name+"], ";
}
remainingPropertiesString += "["+remainingPropertiesList[remainingPropertiesList.length-1].type+" "+
remainingPropertiesList[remainingPropertiesList.length-1].name+"]";
}
console.log(">>>"+functionList.length+" functions:\n"+functionString);
console.log(">>>"+remainingPropertiesList.length+" remainingProperties:\n"+remainingPropertiesString);
//iterates through all properties to determine their type
function checkElem(oneElem, pos, arr) {
console.log("pos: "+pos+" at "+obj+" is: "+oneElem); //actual pos at the encolsing object
try {
if ((typeof obj[oneElem]) === 'function') {
functionList.push({type: (typeof obj[oneElem]), name: oneElem});
} else {
remainingPropertiesList.push({type: (typeof obj[oneElem]), name: oneElem});
}
} catch (e) {
console.log("<<<<<<<error while accessing "+oneElem);
console.log("property exists: "+obj.hasOwnProperty(oneElem));
}
}
}
最佳答案
As I understand it all the typeErrors came from the fact that the different objectPrototypes in the prototype chain have no accessible values, because they are prototypes and no "real" objects.
是的,原型(prototype)属性不应该在原型(prototype)对象本身上调用,而应该在通过构造函数创建的具体实例上调用。
正如您在屏幕截图中看到的那样,ownerGlobal
不是一个值属性,它是一个 getter,因此是一个以原型(prototype)作为 this
对象调用的函数,getter 函数并不是为此而设计的。
你能做的就是获取 property descriptor在原型(prototype)上:
Object.getOwnPropertyDescriptor(somePrototype, propertyName)
,然后就可以提取getter函数和 apply它到一个实例
关于javascript - [需要澄清]如何在探索原型(prototype)属性时处理类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32069185/
接上篇 通过一个示例形象地理解C# async await 非并行异步、并行异步、并行异步的并发量控制 前些天写了两篇关于C# async await异步的博客, 第一篇博客看的人多,点
前言 在 SwiftUI 中,我们可以通过添加不同的交互来使我们的应用程序更具交互性,这些交互可以响应我们的点击,点击和滑动。 今天,我们将回顾SwiftUI基本手势:
今年我一直在想,2022年我想做些什么,做哪方面的改变,这周末在家终于想到了! 2021 轻描淡写 年底就一直想对2021年写一篇总结的,起码不得写个千八百字,可是思来想去不知道怎么写,直到最后都没想
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
在 Eclipse 中使用 Java 进行开发时,它非常方便:您可以像自己一样附加源代码并探索核心 Java 代码。在 Visual Studio 中,我知道只有在调试时才能查看 .net 源代码(我
我正在尝试创建自己的字符串数据类型,谁能告诉我 typedef 和初始化做错了什么。 #include #include typedef char string[10]; int main(){
我期待开发一些东西来分析在服务器上运行的应用程序的 JVM 线程,要求如下: 访问在单独应用程序中运行的所有线程 打印线程栈 了解事件的详细信息 - 记录执行时间和方法详细信息(在特定线程中执行) 我
是否可以探索 Android 内部存储?我需要这个用于调试目的,以帮助我的开发工作。 最佳答案 您可以在模拟器上,或在 Root设备上。只是 adb shell 连接设备,然后从那里导航。 关于and
我有一个使用大量外键的 innoDB 表,但我们只想从中查找一些基本信息。 我做了一些研究,但还是迷路了。 如何判断我的主机是否有 Sphinx已经安装了吗?我没看到作为表格存储的选项方法(即 inn
我有一个创建列表的 GWT 代码(作为结果的网格),我将样式设置为 CSS 类,如 .test tr { height: 26px; } 现在...如果在渲染未完成或网格没有元素时我需要从代码
我需要使用 Javascript 和 HTML 为 Rally 敏捷工具开发一个 View 。我没有处理过在我作为开发人员的新职业中经常使用的网络语言。 我只是在探索他们的 API,但不知道如何探索他
我想了解 Hadoop 而不是一个黑盒子。我想探索 Hadoop 代码本身。我怎样才能不从主干下载 bundle ,我应该从哪里开始?任何帮助都会很有帮助谢谢舒佳特 最佳答案 Hadoop 代码在 S
想象一下这样的情况。您获得了一些遗留代码或获得了一些新框架。您需要尽快调查并了解如何使用此代码。没有机会向以前的开发人员寻求帮助。什么是最佳实践/方法/方式/步骤/工具(首选 .NET Framewo
我注意到我的 git 存储库中的某些 makefile 缺少变量定义的问题,我想搜索所有提交历史以查找我的变量 TESTDIR 在变更集中出现的位置 我该怎么做? 干杯 最佳答案 你可以使用 git
有什么方法可以探索 GO 包吗? 在 java 中,我使用“javap java.lang.String”命令来查看类内部的方法。像这样,有没有命令是他们用 GO 语言写的? 我在谷歌中搜索了相同的内
我注意到 docker 我需要了解容器内发生了什么或其中存在哪些文件。一个示例是从 docker 索引下载图像 - 您不知道图像包含什么,因此无法启动应用程序。 理想的情况是能够通过 ss
近日,华为 分析服务 6.9.0版本发布,正式上线 探索能力 。开发者可自由定义与配置分析模型,支持报告实时预览,数据洞察体验更加灵活与便捷. 新上线的探索能力中,有漏斗分析、事件归因、会话路径分析
我有一个 4 列的 excel 2010 电子表格。 A 列:我销售的产品的 UPC 代码列表。大约300行。 B 列:公式(稍后会详细介绍) C 列:另一个 UPC 代码列表。这些 UPC 代码大约
我有 3 个表格如下: CREATE TABLE USER_STATUS ("UID" varchar2(7), "STAT_ID" varchar2(11)) ; INSERT ALL IN
有什么方法可以探索 java 脚本对象(如 telerik 菜单或任何其他第 3 方对象)的属性和/或功能?我可以通过调试和破坏然后在 watch 中添加对象或在 VS 中使用智能感知来实现。 我使用
我是一名优秀的程序员,十分优秀!