代码记录

5 篇文章 0 订阅
2 篇文章 0 订阅

鼠标特效:

.li-img:hover{
              transform:scale(1.1,1.1);
              -webkit-transition: all 0.5s linear;
              -moz-transition: all 0.5s linear;
              -o-transition: all 0.5s linear;
              transition: all 0.5s linear;
            }
new 的实现原理
function Person(name,age,sex) {
  this.name = name;
  this.age = age;
  this.sex = sex;
}

function newObj(Fn,args) {
  let re = {};
  if(Fn.prototype !== null){
    Object.setPrototypeOf(re,Fn.prototype);
    // re = Object.create(Fn.prototype);
  }
  const result = Fn.call(re,...args);
  if ((typeof result === "object" || typeof result === "function") && result !== null) {
    return result;
  }
  return re;
}

let person = newObj(Person,['zhuhong','24','man']);
console.log(person.prototype);

function New(Fn,...args) {
  const obj = {};
  if(Fn.prototype !== null){
    Object.setPrototypeOf(obj,Fn.prototype);
    // obj = Object.create(Fn.prototype);
  }
  const obj1 = Fn.apply(obj,args);
  if((typeof obj1 === 'object' || typeof obj1 === 'function') && obj1 !== null){
    return obj1;
  }
  return obj;
}

function Person(name) {
  this.name = name;
}

const person = New(Person,'朱宏');
console.log(person);
console.log(person);

Function.prototype.Bind=function () {
  const out = Array.from(arguments);
  const ctx = out.shift();
  const self = this;
  return function (ctx) {
    const inner = Array.from(arguments);
  }
}

const myPromise = new Promise((resolve, reject)=>{
  console.log('hahah');
  // resolve();
  reject();
}).then(()=>{
  console.log('then');
}).catch(()=>{
  console.log('catch')
}).finally(()=>{
  console.log('finally');
})

const a = new Promise((resolve, reject)=>{
  resolve(100);
})
const b = new Promise((resolve, reject)=>{
  reject(1);
}).catch((res)=>{
  console.log(res)
})
const c = new Promise((resolve, reject)=>{
  resolve(25);
})
Promise.race([a,b,c]).then((res)=>{
  console.log(res)
})

const f = () => console.log('now');
// Promise.resolve().then(f);
(async ()=>f())();
console.log('next');

js实现单例模式
function Universe() {
  if(typeof Universe.instance === 'object'){
    return Universe.instance;
  }
  Universe.instance = this;
  return this;
}
function Universe() {
  return function () {
    if(typeof Universe.instance === 'object'){
      return Universe.instance;
    }
    Universe.instance = this;
    return this;
  }();
}

let a = new Universe();
let b = new Universe();
console.log(a===b);
console.log(Universe.instance)

let a = 1;
function f(a) {
  a = 2;
}
f(a)
console.log(a);//a = 1

let a = {a:1}
function f(c){
  c.a = 2;
}
f(a)
console.log(a)//{ a: 2 }
/*
js有原始类型和引用(共享)类型,上述前者属于原始类型,不存在引用(指针)传递,也就是没有深浅拷贝之分,直接是独立变量。
所以函数中的a属于局部变量,改变不了全局变量的值。

后者属于引用(指针)传递,也就是指针指向物理地址是一个,属于浅拷贝,故能改变该对象的值。
*/

'use strict';
let a = 1;
let obj = {
  a: 2,
  b:function () {
    console.log(a)
  },
  print:function () {
    console.log(this.a);
  }
}
obj.b();//a = 1
obj.print();//a = 2
let p = obj.print;
p();//undefined
/*
浏览器严格模式下this全局指向为undefined,对象this指向自己本身
 */

//两个有序数组A和B,判断B是否是A的子序?要求不可以转化数据格式,不可以使用api
let A = [1,2,3,4,5,6,7,8,9,10];
let B = [1,8,9]
// let B = [8,8.5,9]
//使用api
console.log(Array.from(new Set([...A,...B])).length === A.length)
//不使用api
function isChild(A,B){
  if(B.length === 0){
    return true;
  }
  let flag = 0;
  for (let i=0; i<B.length; i++){
    let index = false;
    for (let j=flag; j<A.length; j++){
      if(!index && j === A.length - 1){
        return false;
      }
      if(B[i] === A[j]){
        flag = j;
        index = true;
        break;
      }
    }
  }
  return true;
}
console.log(isChild(A,B));

// 给定长度为N(1≤N≤2000)的字符串S,要构造一个长度为N的字符串T。期初,T是一个空串,随后反复进行下列任意操作。
// ·从S的头部删除一个字符,加到T的尾部
// ·从S的尾部删除一个字符,加到T的尾部
// 目标是要构造字典序尽可能小的字符串
input: acdbcb
output: abcbcd
const input = 'acdbcb';
const output = 'abcbcd';
const strArr = input.split('');
let result = '';

while (strArr.length > 1){
  let left = strArr[0];
  let right = strArr[strArr.length-1];
  if(left < right){
    let head = strArr.shift();
    result = result + head;
  }
  if(left > right){
    let foot = strArr.pop();
    result = result + foot;
  }
  if (left === right){
    result = result +getMin(left,right,strArr);
  }
}

console.log(result+strArr[0])

function getMin(left,right,arr) {
  let h='',f='';
  while(left === right && arr.length > 1){
    h = h + arr.shift();
    f = f + arr.pop();
    left = arr[0];
    right = arr[arr.length-1];
  }
  if(h < f){
    arr.push(f);
    return h;
  } else {
    arr.unshift(h);
    return f;
  }
}

Function.prototype.a = () => console.log(1)
Object.prototype.b = () => console.log(2)
function A(){}
A.prototype.c = () => console.log(3)
var a = new A()
console.log(Object.getPrototypeOf(A.prototype))//Object.prototype
console.log(Object.getPrototypeOf(Function) === Function.prototype)//true
console.log(Object.getPrototypeOf(Array) === Function.prototype)//true
console.log(Object.getPrototypeOf(Object) === Object.prototype)//false
console.log(Object.getPrototypeOf(A) === A.prototype)//false
// a.a() // 报错
a.b() // 2

//输出1该怎么办?
Function.a();
A.a();
A.prototype.c();
a.c();
a.constructor.a();
console.log(a.constructor)
/*
本题检测js原型链的基本知识,a是实例,a的constructor是A,没有prototype。继承A.prototype。
所以a.a()在A.prototype中并没有a()这个方法。那么继续沿着原型链网上找,a.__proto__.__proto__,
也就是A.prototype.__proto__ = Object.prototype,只有b没有a,所以报错a.a is not a function。
此处注意A的__proto__指向Function.prototype,A.prototype指向Object.prototype,a.constructor就是a的构造函数。切记
 */

console.log(a)//undefined
var a = 1
console.log(b)//Cannot access 'b' before initialization
let b = 2
console.log(c)//c(){}
function c(){}

var x = 20
function a(y){
  var x = 10
  return get(y)
}
function get(y){
  return x+y
}
console.log(a(10))//30

// 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
// 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
// 注意: 给定 n 是一个正整数。

//尾递归优化
function num(n,t1=0,t2=1) {
  if(n === 0)return t2;
  return num(n-1,t2,t2 + t1);
}
let start = new Date().getTime();
console.log(num(0));
let end = new Date().getTime();
console.log('递归耗时:',end-start);

num(4,0,1);
num(3,1,1);
num(2,1,2);
num(1,2,3);
num(0,3,5);

function num1(n) {
  if(n < 1){
    return 0;
  }
  if(n === 1){
    return 1;
  }
  let a = 1;
  let b = 2;
  let total = 0;
for(let i=3;i<=n;i++){
  total = a + b;
  a = b;
  b = total;
}
return total;
}
let start1 = new Date().getTime();
console.log(num1(0));
let end1 = new Date().getTime();
console.log('循环耗时:',end1-start1);

const arr = [50,32,5,73,8,6,5,4,8,5,66,88,56,4,3,4,554,45,54,4]
//插入排序
function f(arr){
  let flag = 1;
  for(let i=flag; i<arr.length; i++){
    let num = arr[i];
    for(let j=flag-1; j>=0; j--){
      if(num < arr[j]){
        arr[j+1] = arr[j];
      } else {
        arr[j+1] = num;
        break;
      }
    }
    flag++;
  }
  return arr;
}
console.log(f(arr));

//冒泡排序
function f1(arr) {
  let flag = false;
  while (!flag){
    flag = false;
    for(let i=0; i<arr.length; i++){
      if(arr[i] > arr[i+1]){
        flag = true;
        let temp = arr[i];
        arr[i] = arr[i+1];
        arr[i+1] = temp;
      }
    }
    if(!flag){
      return arr;
    }
  }
}
console.log(f1(arr));

快速排序
function f2(arr,l,r) {
  if(l < r){
  let i = l;
  let j = r;
  let X = arr[i];
  let index = l;
  while (i < j){
    for(j;j>0;j--){
      if(arr[j] < X){
        arr[i] = arr[j];
        index = j;
        break;
      }
    }
    for(i;i<j;i++){
      if(arr[i] > X){
        arr[index] = arr[i];
        index = i;
        break;
      }
    }
  }
  arr[i] = X;
  f2(arr,l,i-1);
  f2(arr,i+1,r);
  }
}
f2(arr,0,arr.length-1)
console.log(arr)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值