Get all functions of an object in JavaScript

前端 未结 4 1808
后悔当初
后悔当初 2021-01-02 14:56

For example,

Math.mymfunc = function (x) {   
    return x+1;
}

will be treated as a property and when I write

for(var p in         


        
4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-02 15:42

    Object.getOwnPropertyNames(Math); is what you are after.

    This logs all of the properties provided you are dealing with an EcmaScript 5 compliant browser.

    var objs = Object.getOwnPropertyNames(Math);
    for(var i in objs ){
      console.log(objs[i]);
    }
    

提交回复
热议问题