Reprinted with 7 Essential JavaScript Functions

Source: Internet
Author: User

Essential JavaScript Functionsby David Walsh Onjune 2, 2015

I Remember the early days of JavaScript where you needed a simple function for just about everything because the browser V  Endors implemented features differently, and not just edge features, basic features, like addEventListener and attachEvent . Times has changed but there is still a few functions each developer should has in their arsenal, for performance for FU Nctional ease purposes.

debounce

The Debounce function can be a game-changer if it comes to event-fueled performance.  If you aren the using a debouncing function with a scroll , resize the key* event, you ' re probably doing it wrong. Here's a debounce function to keep your code efficient:

//Returns a function, that, as long as it continues to be invoked, would not//be triggered. The function would be called after it stops being called for//N milliseconds. If ' immediate ' is passed, trigger the function on the//leading edge, instead of the Trailing.function debounce (func, wait , immediate) {var Timeout;return function () {var context = this, args = Arguments;var later = function () {timeout = Null;i F (!immediate) func.apply (context, args);}; var callnow = immediate &&!timeout;cleartimeout (timeout); timeout = SetTimeout (later, wait); if (Callnow) Func.apply (context, args);};};/ /Usagevar MYEFFICIENTFN = debounce (function () {//All of the taxing stuff do}, Window.addeventlistener (' Resize ', my EFFICIENTFN); 

The function would not allow debounce a callback to being used more than once per given time frame. This was especially important when assigning a callback function to frequently-firing events.

poll

As I mentioned debounce with the function, sometimes you don't get to plug into an event to signify a desired state--if the Event doesn ' t exist, you need-to-check for your desired state at intervals:

function poll (FN, callback, Errback, timeout, interval) {var endTime = number (new Date ()) + (Timeout | |    2000); Interval = Interval | |    100;             (function P () {//If the condition is met, we ' re done!            if (FN ()) {callback ();  }//If the condition isn ' t met but the timeout hasn ' t elapsed, go again else if (number (new Date ())            < EndTime) {setTimeout (P, interval);            }//didn ' t match and too much time, reject!            else {errback (' timed out for ' + fn + ': ' + arguments)); }    })();} Usage:ensure element is Visiblepoll (function () {return document.getElementById (' Lightbox '). offsetwidth &G T    0; }, function () {//Done, Success callback}, function () {//Error, Failure callback}); 

Polling has a long been useful on the web and would continue to being in the future!

once

There is times when you prefer a given functionality only happen once, similar to the the-the-the-the-the-use-an onload event. This code provides said functionality:

Function once (FN, context) {var result;return function () {if (fn) {result = Fn.apply (Context | | this, arguments); fn = nul l;} return result;};} Usagevar canonlyfireonce = once (function () {console.log (' fired! ');}); Canonlyfireonce (); "Fired!" Canonlyfireonce (); Nada

onceThe function ensures a given function can only be called once, thus prevent duplicate initialization!

getAbsoluteUrl

Getting a absolute URL from a variable string is ' t as easy as you think.  There ' s the URL constructor but it can act up if you don't provide the required arguments (which sometimes you can ' t). Here's a suave trick for getting an absolute URLs from and string input:

var Getabsoluteurl = (function () {var a;return function (URL) {if (!a) a = document.createelement (' a '); a.href = Url;return A . href;};}) ();//Usagegetabsoluteurl ('/something '); Http://davidwalsh.name/something

The "Burn" element href handles and URL nonsense for you, providing a reliable absolute URL in return.

isNative

Knowing if a given function is native or not can signal if you ' re willing to override it. This handy code can give you the answer:

;(function () {//used to resolve the internal ' [[Class]] ' of values var toString = Object.prototype.toString;    Used to resolve the decompiled source of functions var fntostring = Function.prototype.toString; Used to detect host constructors (Safari > 4; really typed array specific) var Rehostctor =/^\[object. +?  constructor\]$/;  Compile a regexp using a common native method as a template.  We chose ' object#tostring ' because there ' s a good chance it is not being mucked with.  var renative = RegExp (' ^ ' +//coerce ' object#tostring ' to a string string (toString)//Escape any special REGEXP Characters. Replace (/[.*+?^${} () |[    \]\/\\]/g, ' \\$& ')//Replace mentions of ' toString ' with '. *? ' to keep the template generic.    Replace thing like ' for ... ' to the support environments like Rhino which add extra info//such as method arity. . Replace (/tostring| ( function). *? (? =\\\ () | for. +? (?=\\\]) /g, ' $1.*? ')    + ' $ ');  function Isnative (value) {  var type = typeof value; return type = = ' function '//use ' function#tostring ' to bypass the value ' s own ' toString ' method//and avoid is      ing faked out. ?      Renative.test (Fntostring.call (value))//Fallback to a host object check because some environments would represent      Things like typed arrays as DOM methods which could not conform to the/normal native pattern. : (value && type = = ' object ' && rehostctor.test (Tostring.call (value))) | |  False }//Export however you want module.exports = isnative;} ());//usageisnative (alert); Trueisnative (mycustomfunction); False

The function isn ' t pretty but it gets the job done!

insertRule

We all know so we can grab a NodeList from a selector (via document.querySelectorAll ) and give each of the them a style, but what's more Efficie NT is setting this style to a selector (as you do in a stylesheet):

var sheet = (function () {//Create the <style> tagvar style = document.createelement (' style ');//ADD a media (and/or Media query) Here if you ' d like!//style.setattribute (' media ', ' screen ')//Style.setattribute (' media ', ' only ' screens and ( MAX-WIDTH:1024PX)//WebKit Hack:(style.appendchild (document.createTextNode ("));//Add the <style> element to The pagedocument.head.appendChild (style); return style.sheet;}) ();//Usagesheet.insertrule ("header {float:left; opacity:0.8; } ", 1);

This was especially useful when working on a dynamic, Ajax-heavy site. If you set the "style to a selector", you don't need to account for styling each element, which may match this selector (now O R in the future).

matchesSelector

Oftentimes we validate input before moving forward;  Ensuring a Truthy value, ensuring forms data is valid, etc.  But how often does we ensure an element qualifies for moving forward? You can use a matchesSelector function to validate if an element is a given selector match:

function Matchesselector (el, selector) {var p = element.prototype;var f = p.matches | | p.webkitmatchesselector | | p.mozmat Chesselector | | P.msmatchesselector | | function (s) {return [].indexof.call (Document.queryselectorall (s), this)!==-1;}; Return F.call (el, selector);} Usagematchesselector (document.getElementById (' mydiv '), ' div.someselector[some-attribute=true] ')

There you has it:seven JavaScript functions that every developer should keep in their toolbox.  Have a function I missed? Please share it!

Reprinted with 7 Essential JavaScript Functions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.