使用JavaScript获取对象的所有功能 [英] Get all functions of an object in JavaScript

查看:123
本文介绍了使用JavaScript获取对象的所有功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,

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

将被视为属性,当我写入时

  for(var Math in Math.__ proto__)console.log(p)

会显示。但其余的数学函数不会。我如何获得Math对象的所有功能?

解决方案> Object.getOwnPropertyNames(Math); 。



记录所有提供的符合EcmaScript 5兼容浏览器的属性。

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


For example,

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

will be treated as a property and when I write

for(var p in Math.__proto__) console.log(p)

it will be shown. But the rest of Math functions will not. How can I get all functions of a Math object?

解决方案

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]);
}

这篇关于使用JavaScript获取对象的所有功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆