The example of this article tells a few JS Sohu face test questions. Share to everyone for your reference, specific as follows:
Implement an iterator that iterates through an array or all members of an object.
var each = function (obj, fn) {
//+++++++++++ answer area +++++++++++
//+++++++++++ answer end +++++++++++
};
try{
var data1 = [4,5,6,7,8,9,10,11,12];
var data2 = {
"a": 4,
"B": 5,
"C": 6
};
Console.group (data1);
Each (data1, function (o) {
if (6 = =) return
true;
else if (8 = this) return
false;
Console.log (o + ": \" "+ This +" \ ");
});
Console.groupend ();
/*------[Execution result]------
1: "4"
2: "5"
4: "7"
------------------* * *
console.group (data2);
Each (data2, function (V, n) {
if (5 = =) return
true;
Console.log (n + ": \" "+ V +" \ ");
});
Console.groupend ();
/*------[Execution result]------
A: "4"
C: "6"
------------------/
}catch (e) {
console.error ("Error executing, Error message: "+ E);
}
"Analysis of Ideas"
1. First of all to judge the incoming is an array or object, used to instanceof,typeof and instanceof can be used to determine the type of JS variable, usage difference
typeof (obj)//typeof returns a basic data type
obj instanceof Array//instanceof is typically used to verify that an object belongs to a class
Note: typeof encounters null, Array, objects return object type
var each = function (obj, fn) {
if (obj instanceof Array) {
}
else if (obj instanceof Object) {
}
};
2. The difference between traversing an array and traversing an object
To traverse an array:
for (Var i=0,j=array.length;i<j;i++) {
alert (array[i]);
}
Traversing objects:
for (var e in data) {
alert (data[e]);
}
3. Analysis Results
Each (data1, function (o) {
if (6 = =) return
true; Indicates that skip and continue to traverse
else if (8 = this) return
false; Indicates stop traversal
console.log (o + ": \" "+ This +" \ ");
});
If the direct for loop, that will output all elements of the array, now has a each function, should let him point to the element in obj (that is, change this point, let this represent obj[i])
Fn.call (obj[i],i+1); FN is the second argument for each, so that the function points to the element in obj, the first argument o, let it pass the value i+1
Only this will output 4,5,7,8,9,10,11,12, so you need to limit it to 8 when it jumps out of the loop.
if (obj instanceof Array) {for
(var i=0,j=obj.length;i<j;i++) {
var temp=fn.call (obj[i],i+1);
if (temp===false) { //=== value and type must wait, = = just value the same null==false return
;
}
}}
Similarly, traversing objects
else if (obj instanceof Object) {for
(var e in obj) {
fn.call (obj[e],obj[e],e); First parameter V (object value), second N (object index)
}
}
Note: Obj instanceof object is after obj instanceof array because the array belongs to the object, and the subsequent judgment is not executed
Second, the realization of a class called Man, including attr, words, say three methods.
var Mans;
+++++++++++ Answer area +++++++++++//+++++++++++ answer end +++++++++++ try{var me = man ({fullname: "Little Red"});
var she = new Man ({fullname: "Little Red"});
Console.group ();
Console.info ("My name is:" + me.attr ("fullname") + "\ n My gender is:" + me.attr ("gender"));
Console.groupend ();
/*------[Execution Results]------My name is: Xiao Hong my sex is:< user has not entered >------------------* * me.attr ("FullName", "xiaoming");
Me.attr ("Gender", "male");
Me.fullname = "Waste firewood";
Me.gender = "Simon";
She.attr ("Gender", "female");
Console.group ();
Console.info ("My name is:" + me.attr ("fullname") + "\ n My gender is:" + me.attr ("gender"));
Console.groupend ();
/*------[Execution Results]------My name is: Xiao Ming my gender is: male------------------/Console.group ();
Console.info ("My name is:" + she.attr ("fullname") + "\ n My gender is:" + she.attr ("gender"));
Console.groupend (); /*------[Execution Results]------My name is: Little Red my gender is: female------------------/me.attr ({"Words-limit": 3, "Wo
Rds-emote ":" Smile "}); Me.words ("I like watching videos."
"); Me.words ("Our office is so beautiful.")
"); Me.words ("There are so many beauties in the video!")
"); Me.words ("I usually see Youku!")
");
Console.group ();
Console.log (Me.say ()); /*------[Execution Results]------Xiao Ming smiled: "I like watching video." Our office is so beautiful. There are so many beauties in the video!
"------------------*/me.attr ({" Words-limit ": 2," words-emote ":" Shout "});
Console.log (Me.say ());
Console.groupend (); /*------[Execution Results]------Xiao Ming shouted: "I like watching video." Our office is so beautiful.
"------------------/}catch (e) {console.error (" Execution error, error message: "+ e);}
Thinking Analysis:
1. First come to a constructor
2.
var me = man ({fullname: "Little Red"});
var she = new Man ({fullname: "Little Red"});
For more on JavaScript related content to view the site topics: "JavaScript object-oriented Tutorial", "javascript json operation tips Summary", "JavaScript switching effects and techniques summary", " JavaScript Search Algorithm Skills summary, javascript error and debugging skills Summary, JavaScript data structure and algorithm skills summary, JavaScript traversal algorithm and skills summary and JavaScript mathematical Operational Usage Summary
I hope this article will help you with JavaScript programming.