【问题标题】:Identifying HTML documents in my object - javascript识别我的对象中的 HTML 文档 - javascript
【发布时间】:2016-04-03 01:13:53
【问题描述】:

为了推出我自己的 javascript 对象复制器,我希望能够识别每个属性的类型。这是我的代码:

function HandlerFunction(){
}

function item(){
    this.any=1;
    this.doc=document.createElement('DIV');
    this.onclick=new Function('HandlerFunction()');
}

var o={};
o=new item();
o.any.??? = a number indicating 'any' is a custom object name
o.doc.??? = a number indicating doc is an html object name
o.onclick.??? = a number indicating onclick is an even handler name

是否有一个我可以使用的函数可以替换问号,让我可以将事件处理程序、html 元素和自定义对象名称彼此分开,这样我在迭代时就不会最终进行递归复制我未来的复制功能中的属性?

【问题讨论】:

    标签: javascript function object methods properties


    【解决方案1】:

    您可以将instanceof 用于对象,将typeof 用于基元

    o.doc instanceof HTMLElement // true
    typeof o.any == "number" // true
    o.onclick instanceof Function // true
    typeof o.onclick == "function" // true, function also works with typeof
    

    Which is best to use: typeof or instanceof?

    【讨论】:

    • 我会进一步研究这些函数。
    猜你喜欢
    • 2020-11-28
    • 2014-10-24
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 2021-03-28
    • 2019-01-28
    • 2017-04-15
    • 1970-01-01
    相关资源
    最近更新 更多