Skip to content

Instantly share code, notes, and snippets.

let currentInstance = null  // 缓存当前的vue实例
let isMounting = false      // 初始化挂载 or 更新
let callIndex = 0           // 每个state的唯一标记id

function ensureCurrentInstance() {
  if (!currentInstance) {
    throw new Error(
      `invalid hooks call: hooks can only be called in a function passed to withHooks.`
    )
@xydiva
xydiva / promise-test.md
Created May 31, 2018 14:27
promise测试题
console.log(1);
new Promise(function (resolve, reject){
    reject(true);
    window.setTimeout(function (){
        resolve(false);
    }, 0);
}).then(function(){
    console.log(2);
}, function(){
@xydiva
xydiva / understand-javascript.md
Created May 16, 2018 10:04
大叔手记(19):你真懂JavaScript吗?
if (!("a" in window)) {
    var a = 1;
}
console.log(a); // undefined

变量提升,在代码顶部申明var a = undefined"a" in window 会返回true,if语句不会执行。

@xydiva
xydiva / quick-sort.md
Last active May 14, 2018 09:31
JavaScript中快速排序

最近前端圈闹的沸沸扬扬的快排算法,由于对算法不熟悉,先借鉴高程作者的方案在这里。

function swap(items, firstIndex, secondIndex) {
    var temp = items[firstIndex];
    items[firstIndex] = items[secondIndex];
    items[secondIndex] = temp;
}

function partition(items, left, right) {
@xydiva
xydiva / min-numbers.md
Created May 10, 2018 02:40
给定数字0-9各若干个。你可以以任意顺序排列这些数字,但必须全部使用。目标是使得最后得到的数尽可能小(注意0不能做首位)。例如:给定两个0,两个1,三个5,一个8,我们得到的最小的数就是10015558
function minNumbers() {
    let len = Math.floor(Math.random() * 10 + 3);
    let digit = Math.random().toString().slice(len);
    console.log('给出的数字:', digit)

    let arr = digit.split('');
    let sort = arr.sort(function (a, b) {
        return a - b;
 });
@xydiva
xydiva / deep-clone.md
Last active May 10, 2018 02:37
js对象的深度克隆代码实现(考虑兼容)
// 1
function cloneObject(obj, parent) {
    if (typeof obj !== 'object') {
        return null;
    }

    const o = obj instanceof Array ? [] : {};

 for (let key in obj) {
@xydiva
xydiva / count-string.md
Last active May 10, 2018 02:38
利用字符重复出现的次数,编写一个方法,实现基本的字符串压缩功能(比如:字符串“aabcccccaaa”经压缩会变成“a2b1c5a3”)
function countString(str) {
    const arr = str.split('');
    let n = 1;
    let result = '';
    for (let i = 0; i < arr.length; i++) {
        if (arr[i + 1] === arr[i]) {
            n++;
        }
 else {
@xydiva
xydiva / load-time.md
Last active April 18, 2018 01:27
统计加载时间
(function(){
  const t = window.performance && performance.timing;
  if (!t) {
    return;
  }
  const loadTime = (t.loadEventEnd - t.navigationStart) / 1000;
  alert(`This page loaded in ${loadTime} seconds`);
}())
@xydiva
xydiva / open-new-tab.md
Last active February 8, 2018 07:10
打开后端请求返回的链接窗口

直接打开异步请求到的链接会被浏览器拦截。
可以使用如下解决方案:

async applyForAuth(row) {
    const win = window.open('about:blank', '_blank');
    const r = await getAuth(row.shopId);
    if (r.code === '8001') {
        win.location.href = r.data;
 }
@xydiva
xydiva / update-vue.md
Created February 4, 2018 02:29
更新vue版本
npm install npm@latest -g(可以更新npm至最新版本)  
npm install webpack@latest -S  升级vue的版本  
npm install vue-template-compiler@latest -S 升级compiler的版本