1 Star 1 Fork 0

流沙/JavaScriptAlgorithm

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

JavaScript -- 算法

介绍

JavaScript 常用语法和函数的原生实现,以及熟悉的数据结构、设计模式等用JS实现。

软件架构

待开放

常用语法及函数

软件架构说明

instanceof 实现原理

1. 基本数据类型(利用 Symbol.hasInstance)

class PrimitiveNumber {
  static [Symbol.hasInstance](num){
    return typeof num === 'number'
  }
}

console.log(11 instanceof PrimitiveNumber) // true
console.log(NaN instanceof PrimitiveNumber) // true
console.log('' instanceof PrimitiveNumber) // **  false  **

2. 复杂类型(利用 Symbol.hasInstance)

class ComplexArray{
  static [Symbol.hasInstance](obj) {
    return Object.prototype.toString.call(obj) === '[object Array]'
  }
}

console.log([] instanceof ComplexArray) // true

3. JS 原型链方式实现

function customInstanceOf(leftObj, rightType){
  if (!leftObj || typeof leftObj !== 'object' || !rightType || typeof rightType !== 'function') return false;
  let proto = Object.getPrototypeOf(leftObj);
  while(true) {
    if (proto == null) return false;
    if (proto === rightType.prototype) return true;
    proto = Object.getPrototypeOf(proto);
  }
}

JS 数据类型判断(通用方法)

1. 方法一

function isTargetType(targetType, data){
  if (typeof targetType !== 'string') throw new Error('targetType 必须为字符串')
  
  return Object.prototype.toString.call(data) === '[object '+targetType+']'
}

console.log(isTargetType("Array", [])); // true
console.log(isTargetType('Number', NaN)); // true
console.log(isTargetType('Date', new Date())); // true
console.log(isTargetType('RegExp', /ss/)); // true
console.log(isTargetType('Function', function(){})); // true
console.log(isTargetType('Object', {})); // true
console.log(isTargetType('Object', '111')); // ** false **

2. 方法二 (利用闭包)

const isType = targetType => data => Object.prototype.toString.call(data) === '[object '+targetType+']'

const isArray = isType('Array')
const isRegExp = isType('RegExp')
const isObject = isType('Object')

console.log(isArray([])) // true
console.log(isRegExp(/aaa/)) // true
console.log(isObject({})) // true
console.log(isObject(null)) // ** false **

Array 部分方法原生实现

1. Map 原生实现

Array.prototype.map = function(fn, context){
  const arr = [].slice.call(this);
  const mapArr = [];
  for(let i = 0; i < arr.length; i++){
    if (!arr.hasOwnProperty(i)) continue;
    mapArr[i] = fn.call(context, arr[i], i, this);
  }
  
  return mapArr;
}

var arrSrc = [1,2,3];
arrSrc[4] = 5; // 稀疏数组
var res = arrSrc.map(function (x){return x*x;});
console.log(res, arrSrc) // 原数组不变,产生新的数组

2. forEach 原生实现

Array.prototype.forEach = function(fn, context){
  const arr = [].slice.call(this);
  for (let i=0; i < arr.length; i++){
    if (!arr.hasOwnProperty(i)) continue;
    fn.call(context, arr[i], i, this);
  }
}

3. filter 原生实现

Array.prototype.filter = function(fn,context){
  let arr = [].slice.call(this);
  
  let filterArr = [];
  for(let i=0, len=arr.length; i < len; i++){
    if (fn.call(context, arr[i], i, this)){
      filterArr.push(arr[i])
    }
  }
  
  return filterArr;
}

4. flat 数组扁平化

1. 方法一 (推荐:全部平铺开)
Array.prototype.flatExt = function(){
  let arr = [].slice.call(this);
  let distArr = [];
  let currVal;
  while(arr.length){
    currVal = arr.shift();
    if (Array.isArray(currVal) && arr.unshift(...currVal)) continue;
    
    distArr.push(currVal);
  }
  return distArr
}

var arrSrc = [ 20, 4, 21, [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];
arrSrc.flatExt();
2. 方法二(针对纯数字)
var arrSrc = [ 20, 4, 21, [1, 2, 2], [3, 4, 5, 5], [6, 7, 8, 9, [11, 12, [12, 13, [14] ] ] ], 10];

// 纯数字时
Array.prototype.flatExt = function() { 
  let arr = [].slice.call(this)
  return arr.toString().split(',').map(Number);
}

console.log(arrSrc.flatExt())
  1. 方法三(提供数据平铺深度选择)
// 数组平铺:提供平铺深度. depth 平铺深度 
Array.prototype.flatExt = function(depth=0){
  if (depth === 0) return this;
  
  let arr = [].slice.call(this);
  return arr.reduce((acc, curr, i, thisArr) => {
    if (Array.isArray(curr)) {
      return [...acc, ...([].flatExt.call(curr, depth-1))]
    } else {
      return [...acc, curr]
    }
  }, [])
}

5. every 都满足条件

Array.prototype.every = function(fn, context){
  const arr = [].slice.call(this);
  const len = arr.length;
  let isEvery = true;
  for(let i =0; i<len; i++){
    if (!fn.call(context, arr[i], i, this)){
      isEvery = false;
      break;
    }
  }
  
  return isEvery;
}

Object 部分方法原生实现

1. Object.is 原生实现

Object.is = function(x, y){
  if (x === y){
    return x !== 0 || y !== 0 || 1/x === 1/y;  // 排除 +0 === -0 为 true 问题
  } else {
    return x !== x && y !== y; // 解决 NaN === NaN 为 false 问题
  }
}

2. Object.create 原生实现

Object.create = function(proto, propertiesObject){
  // prototype 或 __proto__ 必须为 object 或 function
  if (typeof proto !== 'object' && typeof proto !== 'function') 
    throw new TypeError('类型不合理');
  
  function F() {}
  F.prototype = proto; // 初始化新对象的 prototype 
  
  let obj = new F();
  proto === null && (obj.__proto__ = proto); // 避免 null,初始化异常
    
  Object.defineProperties(obj, propertiesObject); // 绑定 对象属性
  
  return obj; // 返回新的 obj
}

Function 部分方法原生实现

1. call 原生实现

Function.prototype.call = function(context, ...args){
  if (context === null || typeof context !== 'object') return this(...args);
  
  const fnCaller = Symbol('caller') // 防止直接设置属性时,覆盖context 上的对应属性
  context[fnCaller] = this; // 执行方法做为context的属性方法,确保方法的执行上下文是 context
 
  let res = context[fnCaller](...args); // this 切换到 context
  
  delete context[fnCaller]; // 移除 context 新增属性,避免内存泄漏
  
  return res;
}

//示例:
function fn(age,sex) {
  return `name = ${this.name}, age = ${age}`;
}

console.log(fn.call({name:'重新指定的name'}, '18', '男'))

2. apply 原生实现(原理同 call 的原生实现)

Function.prototype.apply = function(context, args){
  if (context === null || typeof context !== 'object') return this(args);
  
  const fnCaller = Symbol('caller')
  context[fnCaller] = this;
  
  let res = context[fnCaller](args);
  
  delete context[fnCaller];
  
  return res;
}

示例:
function fn(args) {
  return `name = ${this.name}, age = ${args[0]}`;
}

console.log(fn.apply({name:'重新指定的name'}, ['18', '男']))

3. bind 原生实现

Function.prototype.bind = function(context, ...args) {
  return () => {
    return this.call(context, ...args);
  }
}

function fn(age){
  return `name = ${this.name}, age = ${age}`;
}

let fnRes = fn.bind({name:'重新指定的name'}, '18')
console.log(fnRes())

JS 类相关机制

1. js 类及继承 原生实现

// ********* 父类 Animal **********
function Animal(name){
  this.name = name;
  this.getName = function(){ // 私用方法
    return 'name = ' + this.name;
  }
}

// 公共方法
Animal.prototype.getCurrentName = function(){ 
  return 'Animal prototype 方法 : ' + this.name;
}

// 静态方法
Animal.AnimalStaticFn = function(){
  return 'Animal static 方法 : StaticName()';
}

// ********* 子类 Cat 继承 Animal **********
function Cat(name, color){
  Animal.call(this, name); // 继承属性
  this.color = color;
  this.getColor = function(){
    return 'color = ' + this.color;
  }
}

Cat.prototype = Object.create(Animal.prototype); // 继承原型方法
Cat.prototype.constructor = Cat; // 重置子类构造函数

// 公共方法
Cat.prototype.getCurrentColor = function(){
  return 'Cat prototype 方法 : ' + this.color;
}

// 静态方法
Cat.CatStaticFn = function(){
  return 'Dog static 方法 StaticColor()';
}


const catObj = new Cat('Mini_Cat', 'red');
console.log(catObj.__proto__ === Cat.prototype) //true
console.log(catObj.__proto__.__proto__ === Animal.prototype) //true
console.log(Cat.prototype) // 
console.log(catObj.getCurrentName())
console.log(catObj.getCurrentColor())
console.log(catObj.getColor())
console.log(catObj.getName())
console.log(Cat.CatStaticFn())

2. js new 的运行机制及原理

function newFn(constructorFn, ...args){
  if (constructorFn === null || typeof constructorFn !== 'function') return null;
  
  let newObj = Object.create(constructorFn.prototype); // 指定新的对象 __proto__ 指向构造函数原型
  let res = constructorFn.call(newObj, ...args); // constructorFn 执行上下文替换为 newObj
  
  return res;
}

let primitiveObj = newFn(Number, '11') // 简单类型
console.log(primitiveObj)
 
let complexObj = newFn(Object, '11') // 引用类型
console.log(complexObj)

Promise 原生实现

// Promise 状态枚举化
const STATUS_TYPE = {
  PENDING:Symbol('pending'), 
  FULFILLED:Symbol('fulfilled'),
  REJECTED:Symbol('rejected')
}

class PromiseExt{
  status = STATUS_TYPE.PENDING; //【状态】 pending:初始化  fulfilled:完成  rejected:拒绝
  value = undefined; // fulfilled 状态对应的值
  reason = undefined; // rejected 状态对应的原因
  onFulFilledCallbacks = []; // fulfilled 状态对应的所有onFulfilled函数
  onRejectedCallbacks = []; // rejected 状态对应的所有onRejected函数
  
  constructor(handler){
    try{
      handler(this.resolve, this.reject);
    }catch(e){
      this.reject(e)
    }
  }
  
  // 箭头函数,防止在 handler 中执行时,this 的指向异常
  resolve = (value) => {
    if (this.status === STATUS_TYPE.PENDING){
      this.status = STATUS_TYPE.FULFILLED;
      this.value = value;
      while(this.onFulFilledCallbacks.length){
        this.onFulFilledCallbacks.shift()(value)
      }
    }
  }

  reject = (value) => {
    if (this.status === STATUS_TYPE.PENDING){
      this.status = STATUS_TYPE.REJECTED;
      this.reason = value;
      while(this.onRejectedCallbacks.length){
        this.onRejectedCallbacks.shift()(value)
      }
    }
  }

  then(onFulfilled, onRejected) {
    const fnFulfilled = typeof onFulfilled === 'function' ? onFulfilled : value => value;
    const fnRejected = typeof onRejected === 'function' ? onRejected : reason => { throw reason; }
    
    const p2 = new PromiseExt((resolve, reject) =>{
      
      const microtaskFnFulfilled = () => {
        queueMicrotask(() => {
          try{
              let x = fnFulfilled(this.value);
              resolvePromise(p2, x, resolve, reject);
          } catch(e){
              reject(e)
          }
        })
      }
      
      const microtaskFnRejected = () => {
        queueMicrotask(() => {
          try{
              let x = fnRejected(this.reason);
              resolvePromise(p2, x, resolve, reject);
          } catch(e){
              reject(e)
          }
        })
      }
      
      if (this.status === STATUS_TYPE.FULFILLED){
        microtaskFnFulfilled()
      } else if (this.status === STATUS_TYPE.REJECTED) {
        microtaskFnRejected()
      } else {
        this.onFulFilledCallbacks.push(microtaskFnFulfilled);
        this.onRejectedCallbacks.push(microtaskFnRejected);
      }
    })
    
    return p2;
  }

  catch (onRejected){
    this.then(null, onRejected)
  }

  static resolve(value){
    if (value instanceof PromiseExt){
      return value
    } else {
      return new PromiseExt((resolve, reject) =>{ 
        resolve(value)
      })
    }
  }

  static reject(reason) {
    return new PromiseExt((resolve, reject) =>{ 
      reject(reason)
    })
  }
   
}

function resolvePromise(oldPromise, fnRes, resolve, reject){
  
  if (oldPromise === fnRes) { // 返回当前操作的 Promise 时,提示循环执行
    return reject(new TypeError('Chaining cycle detected for promise #<Promise>'))
  }
  
  if (fnRes instanceof PromiseExt){
    fnRes.then(resolve, reject);
  } else {
    resolve(fnRes)
  }
}


// 示例

let promiseExt = new PromiseExt((resolve, reject) =>{
  setTimeout(()=>{
    resolve('AAA');
  }, 1000)
})

promiseExt.then(value=>{
  console.log('延迟返回 value : ' + value)
  return new PromiseExt((resolve, reject)=>{
    resolve('BBB')
  })
}).then(value => {
  console.log('通过Promise 返回 value : ' + value)
  return 'CCC'
}).then(value => {
  console.log('直接返回 value : ' + value)
}).catch(reason=>{
  console.log('reject 03 : ' + reason)
})

数据结构

  1. xxxx
  2. xxxx
  3. xxxx

设计模式

  1. 工厂模式
  2. 单例模式
  3. 适配器模式
  4. 过滤模式
  5. 组合模式
  6. 策略模式
  7. 责任链模式
  8. 观察者模式
  9. 代理模式
  10. 模版模式

前端性能优化

1. 抖动 debounce

function debounce(fn, delay=200){
  let timer;
  return function (...args) {
    if (timer) {
      clearTimeout(timer)
    }
    
    let thisSelf = this;
    timer = setTimeout(() => {
      fn.call(thisSelf, ...args)
    }, delay)
  }
}

2. 节流 throttle

function throttle(fn, delay=200){
  let timer;
  return function(...args) {
    if (!timer){
      let thisSelf = this;
      timer = setInterval(() => {
        fn.call(thisSelf, ...args);
        clearInterval(timer);
      }, delay);
    }
  }
}

3. 图片懒加载机制(未补全)

// Progressive loading images
const imagesToLoad = document.querySelectorAll('img[data-src]');
const loadImages = (image) => {
  image.setAttribute('src', image.getAttribute('data-src'));
  image.onload = () => {
    image.removeAttribute('data-src');
  };
};
if ('IntersectionObserver' in window) {
  const observer = new IntersectionObserver((items) => {
    items.forEach((item) => {
      if (item.isIntersecting) {
        loadImages(item.target);
        observer.unobserve(item.target);
      }
    });
  });
  imagesToLoad.forEach((img) => {
    observer.observe(img);
  });
} else {
  imagesToLoad.forEach((img) => {
    loadImages(img);
  });
}

JavaScript 常用算法

1. 事件总线

1.方法一:(利用ES6语法 ** Class** )

class EventBus {
  constructor(){
    this.eventCenter = {};
  }
  
  on(name, eventHandler) {
    (this.eventCenter[name] || (this.eventCenter[name] = [])).push(eventHandler);
  }
  
  off(name, eventHandler) {
    let handlers = this.eventCenter[name];
    if (!handlers || Object.prototype.toString.call(handlers) !== '[object Array]') return;
    handlers.indexOf(eventHandler) > -1 && handlers.splice(handlers.indexOf(eventHandler), 1)
  }
  
  once(name, eventHandler) {
    let thisSelf = this;
    let handler = function(){
      eventHandler.call(thisSelf, ...arguments)
      
      thisSelf.off(name, handler);
      // delete thisSelf.eventCenter[name];
    }
    
    thisSelf.on(name, handler)
  }
  
  emit(name, ...args){
    let thisSelf = this;
    let handlers = this.eventCenter[name];
    if (!handlers || Object.prototype.toString.call(handlers) !== '[object Array]') return;
    handlers.forEach(handler => {
      handler.call(thisSelf, ...args)
    })
  }
}

// 示例
let fn1 = function(p1, p2){
  console.log('***** fn1:'+ p1 + ' ' + p2)
}

let fn2 = function(p1){
  console.log('***** fn2:'+ p1 )
}

let fn3 = function(p1, p2, p3){
  console.log('***** fn3: ' + p1 + ' ' + p2 + ' ' + p3)
}

let fn4 = function(){
  console.log('***** fn4')
}
 
let fnOnce = function(p1, p2){
  console.log('***** fnOnce: ' + p1 + ' ' + p2)
}

let eventBus = new EventBus()

eventBus.on('click', fn1)
eventBus.on('click', fn2)
eventBus.on('click', fn3)
eventBus.on('input', fn4)
eventBus.once('clickOnce', fnOnce)

eventBus.emit('click', 'a', 'b', 'c', 'd')

console.log('************* 移除 click 事件的部分监听 ')
eventBus.off('click', fn2)
eventBus.emit('click')

console.log('************* 触发 clickOnce 事件 ')
eventBus.emit('clickOnce', 'A', 'F')
console.log(eventBus)

2. 函数柯里化

function curry(fn){
  if (fn.length === 1) return fn;
  
  function fnIteratorCurry(...args){
    if (fn.length === args.length){
      return fn(...args);
    } else {
      return (...nextargs) => {
        return fnIteratorCurry(...nextargs, ...args)
      }
    }
  }
  
  return fnIteratorCurry;
}

let add = curry((x, y, z) => x + y + z);

console.log(add(1)(2)(3));// 6
console.log(add(1)(2, 3)); // 6
console.log(add(1, 2)(3)); // 6
console.log(add(1, 2, 3)); // 6

3. 斐波那契数列

// 1, 1, 2, 3, 5, 8, 13, 21, 34

  1. 方法一(直接递归)
function fibonacci(n){
  if (n <= 0) return ''
  if (n === 1 || n === 2) return 1
  return fibonacci(n-1) + fibonacci(n-2)
}

//示例:
console.time('fibo40')
let res = fibonacci(40)
console.log(res)         // 102334155
console.timeEnd('fibo40') // 820ms

结果:
// 大概执行时间:20->2.5ms 35->85ms; 40->820ms;

缺点:运行时间长,递归容易造成内存溢出
  1. 方法二(利用缓存机制)
function fibo_cache(){
  let fnCache = {};
  const fibo_fnCache = function(n){
    if (!fnCache[n]) {
      if (n <= 0) fnCache[n] = '';
      if (n === 1 || n === 2) fnCache[n] = 1;
      else fnCache[n] = fibo_fnCache(n-1) + fibo_fnCache(n-2);
    }
    
    return fnCache[n];
  }
  
  return fibo_fnCache;
}

let fibonacci_cache = fibo_cache()


//示例:
console.time('fibo40')
let res = fibonacci_cache(40)
console.log(res)         // 102334155
console.timeEnd('fibo40') // 1ms

优缺点:利用缓存机制,缩短计算时间,增加内存占用
  1. 方法三:推荐(利用动态规划)
function fibo_dp(n){
  if (n <= 0) return '';
  let res = 1;
  if (n === 1 || n === 2) return res;
  let pre = 1;
  let curr = 1;
  n = n - 2;
  while(n){
    res = pre + curr;
    pre = curr;
    curr = res;
    n--;
  }
  return res;
}

// 示例:
console.time('fibo40')
let res = fibo_dp(40)
console.log(res)
console.timeEnd('fibo40') // < 1ms

优缺点:计算时间快,占用内存少

4. JS 数据拷贝

  1. 方法一(简单数据拷贝)
function simpleClone(source){
  return JSON.parse(JSON.stringify(source));
}

缺点:只能用于简单的数据拷贝。无法拷贝引用类型、特殊数据类型(函数,Symbol,regExpItem等)、循环引用等情况
  1. 方法二(浅拷贝)
function simpleClone(source){
  return Object.assign( source);
}

缺点:拷贝的是可枚举属性值,深层次的对象,只进行引用的拷贝
  1. 方法二推荐(包括复杂类型:正则,函数,Date,Object,Array 等)
//  **目前未考虑循环引用,或多次引用单个对象的优化** 
var srcObj = {
  strItem: 'AA',
  numItem: 100,
  boolItem: false,
  objItem: {
    objA:'Obj11A',
    objB:'Obj22B',
    objC: {
      C1: 'objc1',
      C2: 'objc2'
    }
  },
  arrItem: [10, '100', {arrObjA:'arrObjA', arrObjB:'arrObjB'}],
  fnItem: function (paraA, optB){
    console.log('***** normal function *****')
  },
  fnItem1: ()=>{console.log('***** arrow function *****')},
  dateItem: new Date(2021, 1, 7),
  regExpItem: new RegExp('abc', 'i'),
  symbolItem: Symbol('symbolLabel')
}


function deepClone(source){
  if (typeof source !== 'object' || typeof source === null)
    return source;
  
  let target = Array.isArray(source) ? [] : {};
  let dataType = '';
  for(let key in source){
    if (!source.hasOwnProperty(key)) continue; // 过滤掉 __proto__ 上的属性
    
    dataType = Object.prototype.toString.call(source[key]);
    if (dataType === '[object Array]' || dataType === '[object Object]') {
      target[key] = deepClone(source[key])
    } else if (dataType === '[object Function]') {
      let fnString = source[key].toString();
      target[key] = source[key].prototype ? eval('(' + fnString + ')') : eval(fnString);
    } else if (dataType === '[object Date]') {
      target[key] = new Date(source[key]);
    } else if (dataType === '[object RegExp]') {
      target[key] = new RegExp(source[key].source, source[key].flags);
      target[key].lastIndex = source[key].lastIndex;
    } else if (dataType === '[object Symbol]'){
      target[key] = Symbol(source[key].description)
    } else {
      target[key] = source[key];
    }
  }
  
  return target;
}

10. Array 简单数据去重

方法一:
let arrSrc = [1, 3, 4, 1, null, undefined, 4, NaN, undefined, null, NaN, '3', true, false, true, 'true']

Array.prototype.distinct = function(){
  let arr = [].slice.call(this);
  let arrDist = []
  let isExistNaN = false;
  for(let i=0; i<arr.length; i++){
    if (arrDist.indexOf(arr[i]) === -1){
      if (Object.is(arr[i], NaN) && isExistNaN) // 避免 NaN 值再次重复
        continue;
      
      arrDist.push(arr[i])
      Object.is(arr[i], NaN) && (isExistNaN = true);
    }
  }
  
  return arrDist;
}

console.log(arrSrc.distinct());

常见面试题

1. a 在什么情况下,能够执行if中的下语句

var a = {
  value: 1,
  valueOf: function(){
    return this.value++;
  }
};

if(a == 1 && a == 2 && a == 3){
  console.log('利用 valueOf 完成目标');
}

2. 请把数据处理为如下结构:[222, 123, null, null, 888, null, null]

var objArr = {1:222, 2:123, 5:888};
objArr.length = 7;

var mapArr = Array.from(objArr).map((value, idx, arr) =>{
  arr[idx] = ((idx !== arr.length -1) && arr[idx+1]) ? arr[idx+1] : null;
  return arr[idx]
})

console.log(mapArr) 

3. 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数

var nums = [1, 11, 7, 15, 6, 8], target = 9;

Array.prototype.findTargetSum = function (targetNum){
  let arr = [].slice.call(this);
  
  let firstVal, secondVal;
  while(arr.length){
    if(arr.length === 1) return [undefined, undefined]
    firstVal = arr.shift();
    secondVal = arr.indexOf(targetNum-firstVal)
    if(secondVal !== -1){
       secondVal = arr[secondVal];
       break;
    }
  }
  
  return [firstVal, secondVal];
}

console.log(nums.findTargetSum(target));

4. int 数据倒序显示(如:输入整型 1234 -> “4321”. 要求:使用递归,不用全局变量,函数只有一个参数)

function reverseInt(value){
    if (value.toString().indexOf('-') === -1){
      value += '-'
    }
    if(value.charAt(0) === '-'){
      return value.substr(1);
    }
    
    let arr = value.split('-')
    arr[0] = arr[0].substring(1)
    arr[1] = value.charAt(0)+arr[1]
    return reverseInt(arr.join('-'))
 }
Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

前端面试 JavaScript 算法 JS语法糖 JS设计模式 展开 收起
README
Apache-2.0
取消

贡献者 (1)

全部

近期动态

不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/YCodeProjects/java-script-algorithm.git
git@gitee.com:YCodeProjects/java-script-algorithm.git
YCodeProjects
java-script-algorithm
JavaScriptAlgorithm
master

搜索帮助