The old way, a good answer here will not write. Just record a bad answer.
The problem exposed this time is to do more projects, all practice. 1. Change this code
The target code
const OBJ = {
name: "Jscoder",
skill: ["es6", "react", "angular"],
say:function () {for
(var i = 0, len = this.skill.length; i < Len; i++) {
setTimeout (function () {
console.log (i);
Console.log (This.skill[i]);
}, 0)
console.log (i);}}
;
Obj.say ();
The first type:
Const OBJ = {
name: "Jscoder",
skill: ["es6", "react", "angular"],
say:function ()
{
var that = this;
for (var i = 0, len = this.skill.length; i < Len; i++)
{
setTimeout (function ()
{
console.log (i); C26/>console.log (That.skill[i]);
}) (i), 0)
console.log (i);}}
;
Obj.say ();
Results:
The interviewer then let me write in the browser, but I used the webstorm, with the node environment, the error, the result is very panic .... Did not dare to hand at last. This code can be executed in the browser.
The second type:
It can be implemented in both node and browser (Chrome):
Const OBJ = {
name: "Jscoder",
skill: ["es6", "react", "angular"],
say:function ()
{
var. = This ;
for (Let i = 0, len = this.skill.length; i < Len; i++)
{
(function () {
setTimeout (function () {
consol E.log (i);
Console.log (That.skill[i])})
(i)
console.log (i);}}
;
Obj.say ();
The result is this.
Both of these results have the desired result, but there is a sequential difference between the Console.log (i) and the outer layer. The second way is to print this, and then print () () the self-executing function.
Should be executed the first script macro task, encountered three () (), the three push the micro-task, when the first macro task is finished, followed by the execution of all the micro-tasks encountered this time.
The first order of printing is because each settimeout is a macro task. But it's a micro-task to include settimeout.
The other more Sao methods I wrote here:
Const OBJ = {name: "Jscoder", Skill: ["es6", "react", "angular"], say:function () {This.skill.forE Ach (function (item,index) {setTimeout (function () {console.log (index) Console.lo
g (item);
},0)})}, Say2:function () {for (var i = 0, len = this.skill.length; i < Len; i++) {
SetTimeout (function (i,item) {console.log (i);
Console.log (item);
},0,i,this.skill[i]);
}}, Say3:function () {Let i = 0;
Const ARR = This.skill.slice (); The function Self_shift () {//shift () method is used to remove the first element of the array and return the value of the first element.
Null returns undefined const item = Arr.shift ();
if (item) {Console.log (i);
Console.log (item);
i++;
SetTimeout (self_shift, 0);
}} setTimeout (self_shift,0);
}
};
Obj.say ();
Obj.say2 (); Obj. Say3 (); Closed, the inside I is not the same as the outside I, plus self-executing const OBJ = {name: "Jscoder", Skill: ["es6", "react", "angular"], say:function
() {for (var i = 0, len = this.skill.length; i < Len; i++) {SetTimeout (((j) = = {
return () = {Console.log (j);
Console.log (This.skill[j]);
}}) (i), 0) Console.log (i);
}
}
};
Obj.say (); Const OBJ = {name: "Jscoder", Skill: ["es6", "react", "angular"], say:function () {var. = this
; for (Let i = 0, len = this.skill.length; i < Len; i++) {setTimeout (function () {Console.log
(i);
Console.log (That.skill[i]);
}, 0) console.log (i);
}
}
}; Obj.say ();2. What other role does handwriting bind,bind have.
This is a very good question, and I have opened another blog to talk about this issue:
https://blog.csdn.net/major_zhang/article/details/79709370 3. When 304 when 200, related browser cache issues.
When the browser requests a URL for the first time, the server-side return status is 200, the content is the resource you requested, and a last-modified attribute marks the last time the file was modified at the end of the service period:
Last-modified:fri, 2006 18:53:33 GMT
When the client requests this URL for the second time, according to the HTTP protocol, the browser transmits the If-modified-since header to the server, asking if the file has been modified after that time:
If-modified-since:fri, 2006 18:53:33 GMT
The server-side program obtains the value of this field first, and then compares with the last modification time of the data on the server, returns the 304 not Modified status code and then stops if the server-side resource does not change. This saves the amount of data transferred and achieves bandwidth savings. When the server-side code changes or restarts the server, the resource is re-emitted, similar to when the first request is returned. (send 200.) This ensures that the resources are not duplicated to the client, and that the client is able to get the latest resources when the server changes.
But Last-modified was not all-powerful, and later in the Http1.1 added the ETag to mark the etag with a higher accuracy
Imagine in this scenario-the client on a resource to save the cache time expired, but in fact the server did not update the resource, if the resource data is very large, the client asked the server to re-send this thing over again, whether it is very wasteful bandwidth and time.
So there's the ETag:
When you request a resource for the first time, the server generates and returns a string in the response header, called the ETag.
After the browser receives the resource, the cache resource will also save the etag and become if-none_match. The etag can be thought of as a unique identifier for a resource, and when you request the resource for the second time, the request head will take the etag,server to compare the resource's current etag with the If-none_match in the request header. Then see if if-modified-since has expired, if the same, tell him directly: You use the local bar, I did not change, will not give you back. So the return of 304,304 is that.
From Cache
Although this is 200, he does not interact with the server at all, pulling the local cache directly.
In addition to the browser cache related to the Expires field, this field is stored in relation to the server, there is no guarantee and the client time unified "problem, http1.1 new Cache-control to define the cache expiration time, if the message appears in the same time Pragma, Expires and Cache-control, will take Cache-control as the standard. Last-modified after the etag is to solve, because if on the server, a resource has been modified, but its actual content has not changed at all, The entire entity is returned to the client (even if there is an identical resource in the client cache) because the last-modified time does not match. 4. Handwritten a jsonp, how to return the data.