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

package method #21

Open
Mr-jili opened this issue Jan 3, 2019 · 0 comments
Open

package method #21

Mr-jili opened this issue Jan 3, 2019 · 0 comments

Comments

@Mr-jili
Copy link
Owner

Mr-jili commented Jan 3, 2019

/**

//data数据
let arr = [
  [
    ["1-7", "2-6"], "4-6", [
      ["2-0", "1-4"],
      ["3-9"], "4-5"
    ]
  ]
];
//递归实现
function flat(arr) {
  const result = [];
  function _flat(arr) {
    arr.forEach(element => {
      if (Array.isArray(element)) {
        _flat(element);
      } else {
        result.push(element);
      }
    })
  }
  _flat(arr)
  return result;
}
console.log(flat(arr))

/**

    <input type="text" name="" id="demo" onkeyup="clearNoNum(this)"/>
    //数字输入匹配
    function clearNoNum(obj) {
      obj.value = obj.value.replace(/[^\d.]/g, ""); //清除“数字”和“.”以外的字符
      obj.value = obj.value.replace(/\.{2,}/g, "."); //只保留第一个. 清除多余的
      obj.value = obj.value.replace(".", "$#$").replace(/\./g, "").replace("$#$", ".");
      obj.value = obj.value.replace(/^\./g, ""); //验证第一个字符是数字而不是.
      obj.value = obj.value.replace(/^(\-)*(\d+)\.(\d\d).*$/, "$1$2.$3"); //只能输入两个小数
      if (obj.value.indexOf(".") < 0 && obj.value != "") {
        //以上已经过滤,此处控制的是如果没有小数点,首位不能为类似于 01、02的金额
        obj.value = parseFloat(obj.value);
      }
    }
    //空格处理
    function clearNoNum(obj) {
      obj.value = obj.value.replace(/\s/g, ""); 
    }
/**
 * @author
 * @description:深拷贝
 **/
//递归
function deepClone(obj) {
  let result = Array.isArray(obj) ? [] : {};
  if (obj && typeof obj === 'object') {
    for (let key in obj) {
      if (obj.hasOwnProperty(key)) {
        if (obj[key] && typeof obj[key] === 'object') {
          result[key] = deepClone(obj[key])
        } else {
          result[key] = obj[key]
        }
      }
    }
  }
  return result;
}

//1.JSON.parse与JSON.stringify
/2.通过jQuery的extend方法实现深拷贝
   var array = [1,2,3,4];
   var newArray = $.extend(true,[],array);
//3.Object.assign()拷贝
//当对象中只有一级属性,没有二级属性的时候,此方法为深拷贝,但是对象中有对象的时候,此方法,在二级属性以后就是浅拷贝

/**
 * @author
 * @description:bind封装
 **/
Function.prototype.bind2 = function (context) {
  var self = this;
  return function () {
    self.apply(context);
  }
}
/**
 * @author
 * @description:promise
 **/
class Promise {
  callbacks = [];
  failbacks = [];
  constructor(fn) {
    fn(this.resolve.bind(this), this.reject.bind(this));
  }
  resolve(res) {
    if (this.callbacks.length > 0) this.callbacks.shift()(res, this.resolve.bind(this), this.reject.bind(this));
  }
  reject(res) {
    this.callbacks = [];
    if (this.failbacks.length > 0) this.failbacks.shift()(res, this.resolve.bind(this), this.reject.bind(this));
  }
  catch (fn) {
    this.failbacks.push(fn);
  }
  then(fn) {
    this.callbacks.push(fn);
    return this;
  }
}
/**
 * @author
 * @description:new封装
 **/
function create() {
  // 创建一个空的对象
  let obj = new Object()
  // 获得构造函数
  let Con = [].shift.call(arguments)
  // 链接到原型
  obj.__proto__ = Con.prototype
  // 绑定 this,执行构造函数
  let result = Con.apply(obj, arguments)
  // 确保 new 出来的是个对象
  return typeof result === 'object' ? result : obj
}
/**
 * @author
 * @description:extends实现
 **/
//子类  extends  父类
Function.prototype.extends = function(func, options){
  for(var key in func.prototype){
      this.prototype[key] = func.prototype[key];
  }
  for(var name in options){
      this.prototype[name] = options[name];
  }
}
/**
 * @author
 * @description:高级递归排序
 **/
function quickSort(arr) {
    if(arr.length <= 1) {
        return arr;  //递归出口
    }
    var left = [],
        right = [],
        current = arr.splice(0,1); 
    for(let i = 0; i < arr.length; i++) {
        if(arr[i] < current) {
            left.push(arr[i])  //放在左边
        } else {
            right.push(arr[i]) //放在右边
        }
    }
    return quickSort(left).concat(current,quickSort(right));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant