1. Change the content of the page title
Sometimes we need to dynamically update the name of the title when we develop the H5 page, this time using
document.titile=‘修改后的名字‘;
can solve our problems.
or use
//当前firefox对 title 参数不支持 history.pushstate(state,title,url);
This approach not only modifies the title but also modifies the value of the URL and stores that information in the browser's history stack, which gives the user a better experience when using the return button.
When we do a non-refresh update page data, you can use this method to record the state of the page, so that the page can be rolled back.
2. Logging Synchronous sending requests
There is a scenario like this:
When we do e-commerce products, we want to count the number of hits per product (which is actually a request for departure ajax ). PC-side interaction is mostly a new page after clicking on the product. This time ajax is 同步发送 or 异步发送 does not affect the statistics.
But nested in the client, long is the page that jumps in the current tab . If we still use asynchronous ajax requests, requests are blocked and the results are inaccurate.
3.JavaScript in this related
This part of the content has been seen many times before, but can not understand the deep meaning. Later saw more, also understand.
varAB = {' A ':1,' B ':2,' C ':3Abc: function(){ //Object method, this is the current object of the binding varthat= This; Console.log (' abc ');varAaa= function(){ //that is pointing to the current objectConsole.log (THAT.A);The value of this in the //function is bound to the global window objectConsole.log ( This); }; AAA (); }};console.log ('---------'); Ab.abc ();
The output from the above code browser is as follows:
var BBB=function(){ var a=0; this.b=1; returnthis; } varnew BBB();
Enter the content in the browser to view the output:
Let's make a change to the code as follows:
var BBB=function(){ var a=0; this.b=1; } varnew BBB();
The same input as above to see what the output is
The execution of the new operator is as follows:
- A new object is created. It inherits from
BBB.prototype .
- The constructor
BBB is executed. When executed, the corresponding delegate is passed in, and the context is this specified as the new instance. new BBBequal to new BBB() , can only be used in cases where no parameters are passed.
-
If the constructor returns an "object", the object replaces the entire new result. If the constructor does not return an object, the result of new is the object created in step 1.
In general, constructors do not return any values, but if the user wants to override the return value, they can choose to return a normal object to overwrite. Of course, the returned array is also overwritten because the array is also an object.
4.JavaScript Closure related
A function defined in a closure can "remember" the environment in which it was created.
var test=< Span class= "hljs-function" >function (string) { return function () { console.log (string); }}; var tt=test (); TT ();
//li列表点击每一行 显示当前的行数 var add_li_event=function(node){ var helper=function(i){ returnfunction(e){ alert(i); } }; for (var0, len =node.length; i < len; i++) { node[i].onclick=helper(i); }};
5. Destroying event bindings
I myself in writing JS event binding also went through a process, just at the beginning,,, onclick , bind live delegate on such a process.
The reason for this requirement is that the DOM on our page is dynamically updated. For example, a piece of content is clicked on the content of the page, and then the use of this new content is click definitely not enough to meet the needs.
liveAnd an delegate earlier version of the event delegate (proxy event). The latest version of jquery is used on to do proxy events. More efficient than live and delegate higher.
liveis to bind the event to the current one document , and if the document element is nested too deep, it affects performance during the bubbling process.
delegateand on The difference is
jQueryObject.delegate( selector , events [, data ], handler ) //或者 jQueryObject.delegate( selector, eventsMap )
jQueryObject.on( events [, selector ] [, data ], handler ) //或者 jQueryObject.on( eventsMap [, selector ] [, data ] )
It can be said that the selector of the on descendant element is optional. However delegate , the selector is required. onthan delegate the more flexible.
Many times we are only declaring event bindings, regardless of the event's destruction. However, when writing the front-end plugin, we need to provide the method of event destruction, which is provided to the plug-in consumer. The advantage of this is that the user is more controllable on the plugin, freeing up memory, and providing the performance of the page.
varthat={}; $('. Event_dom '). On (' click ','. Childk_dom ', function(){}); $ (window). On (' scroll ', scrollevent);varScrollevent= function(){};//Event Destructionthat.desrory= function(){$('. Event_dom '). Off ();the destruction of the//window method must use the event name and callback function, which is primarily a window that may bind this system to custom events and back out$ (window). Off (' scroll ', scrollevent); };
If you feel good, please visit the GitHub (point me) address to give me a star. Thank you!
Mobile H5 Development Related content summary--javascript Chapter