Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

一些常用的函数 #33

Open
sundjly opened this issue Mar 4, 2020 · 0 comments
Open

一些常用的函数 #33

sundjly opened this issue Mar 4, 2020 · 0 comments
Labels

Comments

@sundjly
Copy link
Owner

sundjly commented Mar 4, 2020

  1. 判断 web 平台
const detectDeviceType = () =>
  /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
    ? 'Mobile'
    : 'Desktop';

// 事例
detectDeviceType(); // "Mobile" or "Desktop"
  1. 缓存函数:
export const cached = (fn: any) => {
  // Create an object to store the results returned after each function execution.
  const cache = Object.create(null);
  // Returns the wrapped function
  return function cachedFn(str: any) {
    // If the cache is not hit, the function will be executed
    if (!cache[str]) {
      const result: any = fn(str);
      // Store the result of the function execution in the cache
      cache[str] = result;
    }

    return cache[str];
  };
};
@sundjly sundjly added the 分享 label Mar 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant