简体   繁体   English

如何在Javascript中找到属性的所有者

[英]How to find the owner of a property in Javascript

Ok, because my initial question sounds unclear, so I decided to edit it. 好的,因为我最初的问题听起来不清楚,所以我决定编辑它。 My question is how do you find out who defined a certain property, for example, the parseInt function, how do I know on which object it was definded like if parseInt was definded on the window object or the document object or whatever object it is? 我的问题是你如何找出谁定义了某个属性,例如, parseInt函数,我如何知道它被定义的对象,如果parseInt是在window对象或document对象或它的任何对象上定义的? Thank you 谢谢

I know the parseInt was definded the window object, I am just using it as an example in general, I am not specifically asking what object definded the parseInt property. 我知道parseInt被定义为window对象,我只是用它作为一个例子,我并没有具体询问什么对象定义了parseInt属性。

Also, please don't show me jQuery codes since I don't know jQuery that very good. 另外,请不要向我展示jQuery代码,因为我不知道jQuery非常好。

There is unfortunately no way to determine using code what the variable environment is of a given variable. 遗憾的是,没有办法确定使用代码变量环境是给定变量的。

As for object properties, they should be obvious if they are myObj.property . 至于对象属性,如果它们是myObj.property ,它们应该是显而易见的。 If not obvious, it could be possible to use an exhaustive search to look for their existence in certain places, or certain known recursively. 如果不是很明显,则可以使用穷举搜索来查找它们在某些地方的存在,或者某些已知的递归搜索。

Overall, it is not possible to know without looking at implementation documentation. 总的来说,如果不查看实施文档,就无法知道。

I know that to solve my question, we could use Object.prototype.hasOwnProperty() , but that would be very alot of typing because you have to type it out each time you need to know if a certain property is defined on a object. 我知道要解决我的问题,我们可以使用Object.prototype.hasOwnProperty() ,但这会非常类型,因为每次你需要知道是否在对象上定义了某个属性时你必须输入它。 I have decided to write my own function to make this a little easier even though this is of no good practical use, I just wanted to satisfy my curiosity. 我已经决定编写自己的函数来使它变得更容易,即使这没有很好的实际用途,我只是想满足我的好奇心。

function findOwner(property, ownerObjectArray) {
    var result = []; // Array to store the objects that the given property is defined on

    for (var i = 1; i < arguments.length; i++)
    {
        var obj = arguments[i]; // the object currently being inspected
        var properyList= Object.getOwnPropertyNames(arguments[i]); // a list of all "Owned" properties by this object

        for (var j = 0; j < properyList.length; j++)
        {
            if (property === properyList[j]) result.push(obj.constructor);
        }
    }
                return result.length > 0 ? result : "undefinded";
} 

run this method 运行此方法

window.onload = run;

    function run()
    {
        alert(findOwner("parseInt", Array.prototype, window, document));    // passing 3 objects we want to test against to this method. It printed : [object Window], the given property "parseInt" was found on the "Window" object
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM