I won't say much nonsense, go straight to the topic, you, the specific code is as follows:
<script> //-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Object.prototype.clone = function(){}; } // for(var i in man){ if (man.hasOwnProperty(i)) { //filter, only outputs the private attributes of man's console.log(i,":",man[i]); }; } //The output is print hands:2,legs:2,heads:1 for(var i in man) {//No filtering console.log(i,":",man[i]); } //The output is //hands : 2 index.html:20 //legs : 2 index.html:20 //heads : 1 index.html:20 //clone : function (){} for(var i in man) { if(Object.prototype.hasOwnProperty.call(man,i)) { //Filter console.log(i,":",man[i]); } } //The output result is print hands:2,legs:2,heads:1 </script>Next, I will introduce js recursive traversal objects, arrays, and properties.
When working in front-end, sometimes we need to iterate over some objects of unknown types. The code is as follows:
//js traversal object
function TraversalObject(obj)
{
for (var a in obj) {
if (typeof (obj[a]) == "object") {
TraversalObject(obj[a]); //Recursive traversal
}
else {
alert(a + "=" + obj[a]);//The value will be displayed
}
}
}
//Transfer the values of all Ur in the object
function TraversalObject(obj)
{
for (var a in obj) {
if(a=="Url") alert(obj[a]);/ / Show the URL value
if (typeof (obj[a]) == "object") {
TraversalObject(obj[a]); //Recursive traversal
}
}
}
This traversal method plays a very good role when the object is irregular but needs to be obtained the same attributes.