“ Now that you know how to use it for loop , Now I'll teach you how to use this technique to traverse an object .” Ye Lao said .
“ Objects are nothing more than properties and functions , You mean give me an object , I have a way to get all the data in it ( Key value pair ) Do you ?”

“ No mistake , Because suppose there is such a scenario , I need to judge an object , What are attributes , Those things are functions ? Then I need to get all the things in this object in turn , Then determine who is the attribute and who is the function .”
“ wait , Even if you get these things , How do I judge who is an attribute , Who is a function , I don't seem to have this spell yet .”
“ never mind , This is simple , You only need one typeof Keywords are OK . such as , I now have a string and a function .” Say , Ye Lao wrote the following code :
var a = "123"; var fun = function(){ }
“ then , use typeof Keywords to include , Output look .”
var a = "123"; var fun = function(){ } console.log( typeof(a) ); console.log(
typeof(fun) );

“ Did you see? , In this way, you can get the type of variable .a Is a string , therefore typeof Come out string,fun Is a function , therefore typeof Come out function. next , Let me tell you how to traverse an object . first , Create a new simple JS object .”
var yeXiaoFan = { name : " Ye Xiaofan ", age : 16 , eat : function(){ console.log("KFC")
; } }
then , use for Loop to traverse .
for(var p in yeXiaoFan){ console.log(p); }
result :


“ In this for In circulation , It is different from the previous writing . among ,p Is a random name , representative yeXiaoFan The property name traversed from the object . In this way , I can do this without knowing what properties the object has in advance , Get the names of the attributes .” Ye Lao said slowly .
“ Except for the attribute name , Can I get the value of the attribute ?” Ye Xiaofan blinked twice , Asked Ye Lao curiously .
“ You got all the attribute names , Don't you have an attribute value yet ?” Ye Laoyi blows his beard , Asked with a smile .

“ ah , Oh, I see , Now that you have the attribute name , Then the object can directly obtain the value of the attribute in the form of points . of course , Brackets are also OK .” Ye Xiaofan is not stupid , Suddenly realized . See him behave like this , Ye Lao couldn't help nodding .
“ you 're right , That's true . We just need to modify the code a little .” Say , Ye Lao plays another code stream :
var yeXiaoFan = { name : " Ye Xiaofan ", age : 16 , eat : function(){ console.log("KFC")
; } } for(var p in yeXiaoFan){ console.log(p + "=" + yeXiaoFan[p]); }
result :
name= Ye Xiaofan age=16 eat=function (){ console.log("KFC"); }

“ that's enough , But why didn't you just use the point number ?” Ye Xiaofan muttered , But on second thought, I understood the reason . Because the property name traversed is not certain , But with a p Variable denotation , Since it's a variable , Naturally, you can't use a dot . Because if it's written yeXiaoFan.p, Then it will be considered to be looking for a name called p Properties of , But in fact ,p It's just the name of a variable . let me put it another way ,p It doesn't matter what you call it , It's just the name of a variable anyway , What really matters is not p What's the name of the variable , But p What does the variable refer to ?

“ I saw your face , You already know , you 're right , Your conjecture is correct . In case of uncertain attribute name , You can only use one variable instead , let me put it another way , Can't use some , Only brackets can be used . therefore , When an object accesses properties , Brackets are more flexible .”
“ that , Should I always use square brackets , No more counting ?”

“ Not necessarily , Some cases , Or in most cases , Or a dot . Because in most cases , You already know the name of the attribute , Then there is no doubt , It is more convenient to use a dot , You say so .”
Ye Xiaofan thought for a moment , Then he nodded .

Technology