Enumerating shadowed


/*
    :
Custom properties that shadow [[DontEnum]] properties on Object.prototype are not enumerated using for-in. In the following example toString is a property available on Object.prototype and is shadowed on cowboy. Since such properties are not enumerated through for-in, it is not possible to transfer them from a one object to another using for-in.
*/
function cowboy() 
 { 
		this.toString = function () 
		{
			return "cowboy"; 
		}
		this.shoot = function ()
		{ 
			return "bang!";
		} 
}
var p = new cowboy();
document.write("Enumerable properties:"); 
for (var i in p)
{ 
	document.write(" ", i);
} 
document.write("<br/>cowboy propertyIsEnumerable(\"toString\"): ", p.propertyIsEnumerable("toString")); 
document.write("<br/>cowboy propertyIsEnumerable(shoot): ", p.propertyIsEnumerable("shoot")); 
document.write("<br/>cowboy hasOwnProperty(\"toString\"): ", p.hasOwnProperty("toString"));
/*
IE: Enumerable properties: 
shoot cowboy propertyIsEnumerable("toString"):false 
cowboy hasOwnProperty("toString"): true 
FF: Enumerable properties: 
toString shoot cowboy propertyIsEnumerable("toString"): true c
owboy hasOwnProperty("toString"): true
Opera: same as FF 
Safari: same as FF
*/

좋은 웹페이지 즐겨찾기