一个简单的测试看
function F(){}; f = new F(); for (prop in f) alert(prop);
结果并没有alert什么属性,控制台上可以看到下面的对象。
F __proto__: F constructor: function F(){} arguments: null caller: null length: 0 name: "F" prototype: F constructor: function F(){} __proto__: Object __proto__: function Empty() {} apply: function apply() { [native code] } arguments: null bind: function bind() { [native code] } call: function call() { [native code] } caller: null constructor: function Function() { [native code] } length: 0 name: "Empty" toString: function toString() { [native code] } __proto__: Object <function scope> <function scope> Global: Window __proto__: Object __defineGetter__: function __defineGetter__() { [native code] } __defineSetter__: function __defineSetter__() { [native code] } __lookupGetter__: function __lookupGetter__() { [native code] } __lookupSetter__: function __lookupSetter__() { [native code] } constructor: function Object() { [native code] } hasOwnProperty: function hasOwnProperty() { [native code] } isPrototypeOf: function isPrototypeOf() { [native code] } propertyIsEnumerable: function propertyIsEnumerable() { [native code] } toLocaleString: function toLocaleString() { [native code] } toString: function toString() { [native code] } valueOf: function valueOf() { [native code] }
改改上面的代码如下:
function F(){this.attr="test"}; f = new F(); for (prop in f) alert(prop);
这是就会alert出attr.