码迷,mamicode.com
首页 > 其他好文 > 详细

方法和属性的输出

时间:2017-07-13 01:08:06      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:sort   index   push   javascrip   reg   highlight   pre   names   function   

输出实例的属性和方法以及prototype中的属性和方法,主要利用的getOwnpropertyNames()

function getper(obj) {

  console.log(‘------------ 实例 ------------‘);
  var arr_property = [];
  Object.getOwnPropertyNames(obj).sort().forEach(function(item, index) {
    if (typeof obj[item] === ‘function‘) {
      arr_property.push(‘function: ‘ + item)
    } else {
      arr_property.push(‘property: ‘ + item)
    }
  })
  arr_property.sort().forEach(function(value,i) {
    console.log(value);
  })

  console.log(‘------------ prototype ------------‘);
  var arr_prototype = [];
  Object.getOwnPropertyNames(obj.prototype).sort().forEach(function(item, index) {
    if (typeof obj.prototype[item] === ‘function‘) {
      arr_prototype.push(‘function: ‘ + item)
    } else {
      arr_prototype.push(‘property: ‘ + item)
    }
  })
  arr_prototype.sort().forEach(function(value,i) {
    console.log(value);
  })

}

//这里以正则为例
getper(RegExp);

  

方法和属性的输出

标签:sort   index   push   javascrip   reg   highlight   pre   names   function   

原文地址:http://www.cnblogs.com/liangcheng11/p/7158006.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!