How to list the functions/methods of a javascript object? (Is it even possible?)

后端 未结 4 1426
梦谈多话
梦谈多话 2020-12-16 18:54

This question is intentionally phrased like this question.

I don\'t even know if this is possible, I remember vaguely hearing something about some properties not en

4条回答
  •  盖世英雄少女心
    2020-12-16 19:25

    You should be able to enumerate methods that are set directly on an object, e.g.:

    var obj = { locaMethod: function() { alert("hello"); } };
    

    But most methods will belong to the object's prototype, like so:

    var Obj = function ObjClass() {};
    Obj.prototype.inheritedMethod = function() { alert("hello"); };
    var obj = new Obj();
    

    So in that case you could discover the inherited methods by enumerating the properties of Obj.prototype.

提交回复
热议问题