2016-10-12 3 views
2

나는 간단한 개체가?목록 (계산) 프로토 타입 기능 예를 들어

계산 객체 'A'기능 : 당신은 프로토 타입에 Object.getOwnPropertyNames을 사용할 수 있습니다

var a = {}; 
var prototypeFunctions = 0; 
var props = Object.getOwnPropertyNames(a.constructor.prototype); 

for(var i=0; i<props.length; i++){ 
    if(a.constructor.prototype[props[i]] instanceof Function){ 
     prototypeFunctions++; 
    } 
} 

console.log("Number of object 'a' prototype functions: ", prototypeFunctions); 
+1

https://jsfiddle.net/adeneo/wLb5dyrj/ – adeneo

답변

3

. 예를 들어

:

var a = {}; 
 
console.log(Object.getOwnPropertyNames(a.constructor.prototype));

출력 :

[ 
    "__defineGetter__", 
    "__defineSetter__", 
    "hasOwnProperty", 
    "__lookupGetter__", 
    "__lookupSetter__", 
    "constructor", 
    "toString", 
    "toLocaleString", 
    "valueOf", 
    "isPrototypeOf", 
    "propertyIsEnumerable", 
    "__proto__" 
] 

이 프로토 타입의 모든 직접 등록 정보를 나열합니다, 부모로부터 상속되지 않은.

+1

'getPrototypeOf'를 사용합니다 - 생성자가 없습니다. – georg

+0

@georg 'constructor'이 null 일 수있는 환경이 무엇인지 잘 모르겠습니다. , 수동으로 null로 설정하는 것 외에도 (그리고 그 시점에서 프로그래머가'Object.getPrototypeOf'를 null로 설정한다고 가정 할 수도 있습니다.). – tcooc

+0

예를 들어,'a = Object.create (null); b = Object.create (a)' – georg