Skip to content

Instantly share code, notes, and snippets.

View maculosa's full-sized avatar
🎯
Focusing

mo19 maculosa

🎯
Focusing
View GitHub Profile
@maculosa
maculosa / verifyCode.vue
Created October 5, 2021 00:30
[Message Verify Code with Vue] #vue #verifyCode
<template>
<timer-btn @click.native="sendCode()" class="btn btn-default" :disabled="disabled" @run="sendCode" ref="btn"
:second="60"></timer-btn>
</template>
<script>
// 登录验证的按钮组件
Vue.component('timerBtn', {
template: `<button type="button" :disabled="disabled || time > 0 " style="white-space: nowrap;font-size: 13px;">{{text}}</button>`,
// 初始化时间
@maculosa
maculosa / crypto-aes.js
Last active September 25, 2021 03:36
[Crypto AES 加解密算法] #crypto-js #AES #加密算法
import CryptoJS from 'crypto-js';
export class CryptoAES {
constructor(word, secretKey, params) {
this.secretKey = this.parseEncode(secretKey);
this.params = this.params || {
mode: CryptoJS.enc.ECB,
padding: CryptoJS.enc.Pkcs7
}
this.word = this.parseEncode(word);
@maculosa
maculosa / index.css
Created August 4, 2021 01:47
[Timeline] 时间线样式 #timeline #CSS
body {
margin: 0;
padding: 0;
background: rgb(230,230,230);
color: rgb(50,50,50);
font-family: 'Open Sans', sans-serif;
font-size: 112.5%;
line-height: 1.6em;
}
@maculosa
maculosa / error_demo.go
Created August 3, 2021 01:52
[自定义错误] #golang #error
package main
import (
"errors"
"fmt"
"math"
)
func circleArea(radius float64) (float64, error) {
if radius < 0 {
@maculosa
maculosa / bubble_sort.go
Created August 2, 2021 03:50
[冒泡排序] (Bubble Sort) 依次比较两个相邻的元素,如果他们的顺序(如从大到小)就把他们交换过来。
package main
import "fmt"
func main() {
/*
数组的排序
让数组中的元素具有一定的顺序。
arr := [5]int{15,23,8,10,7}
@maculosa
maculosa / memorize.js
Created July 30, 2021 01:22
[记忆] 在函数式编程当中,可以将上次的计算结果缓存起来的技巧叫做 “记忆” #记忆 #缓存
// 返回f()的带有记忆功能的版本
// 只有当f()的实参的字符串表示都不相同时它才会工作
// 记忆的本质是:牺牲算法的空间复杂度以换取更优的时间复杂度,在客户端JavaScript中代码的执行时间复杂度往往成为瓶颈,因此在大多数场景下,这种牺牲空间换取时间的做法以提升程序执行效率的做法是非常可取的
function memorize(f) {
var cache = {}; // 将值保存在闭包内
return function() {
// 将实参转换为字符串形式,并将其用作缓存的键
var key = arguments.length + Array.prototype.join.call(arguments, ",");
if (key in cache) return cache[key];
else return cache[key] = f.apply(this, arguments);
@maculosa
maculosa / jsonStringify.js
Created July 28, 2021 01:11
[实现 JSON.stringify] JSON.stringify([, replacer [, space]) 方法是将一个 JavaScript 值(对象或者数组)转换为一个 JSON 字符串。此处模拟实现,不考虑可选的第二个参数 replacer 和第三个参数 space,如果对这两个参数的作用还不了解,建议阅读 MDN 文档。
/*
基本数据类型:
undefined 转换之后仍是 undefined(类型也是 undefined)
boolean 值转换之后是字符串 "false"/"true"
number 类型(除了 NaN 和 Infinity)转换之后是字符串类型的数值
symbol 转换之后是 undefined
null 转换之后是字符串 "null"
string 转换之后仍是string
NaN 和 Infinity 转换之后是字符串 "null"
@maculosa
maculosa / Object_assign.js
Created July 28, 2021 01:10
[实现 Object.assign]
Object.assign2 = function(target, ...source) {
if (target == null) {
throw new TypeError('Cannot convert undefined or null to object')
}
let ret = Object(target)
source.forEach(function(obj) {
if (obj != null) {
for (let key in obj) {
if (obj.hasOwnProperty(key)) {
ret[key] = obj[key]
@maculosa
maculosa / Object_create.js
Created July 28, 2021 01:09
[实现 Object.create] Object.create()方法创建一个新对象,使用现有的对象来提供新创建的对象的__proto__。
Object.create2 = function(proto, propertyObject = undefined) {
if (typeof proto !== 'object' && typeof proto !== 'function') {
throw new TypeError('Object prototype may only be an Object or null.')
if (propertyObject == null) {
new TypeError('Cannot convert undefined or null to object')
}
function F() {}
F.prototype = proto
const obj = new F()
if (propertyObject != undefined) {
@maculosa
maculosa / instanceof.js
Created July 28, 2021 01:08
[实现 instanceof 关键字] instanceof 就是判断构造函数的 prototype 属性是否出现在实例的原型链上。
function instanceof(left, right) {
let proto = left.__proto__
while (true) {
if (proto === null) return false
if (proto === right.prototype) {
return true
}
proto = proto.__proto__
}
}