Skip to content

Instantly share code, notes, and snippets.

View DefectingCat's full-sized avatar
🎮
ELDEN RING

Sonetto DefectingCat

🎮
ELDEN RING
View GitHub Profile
@DefectingCat
DefectingCat / valueOf.js
Created June 26, 2021 06:24
Number.valueOf
let x = 2;
Number.prototype.valueOf = function () {
return x++;
};
const a = new Number(2);
console.log(a == 2 && a == 3 && a == 4);
const arr = [1, 2, 3, 'x', 'f', 'y'];
// for (const i of arr) {
// console.log(i);
// }
const it = arr[Symbol.iterator]();
for (let res; (res = it.next()) && !res.done; ) {
console.log(res.value);
@DefectingCat
DefectingCat / isWeChat.js
Created March 24, 2022 02:11
Detect whether opened page with WeChat
const isWeChat = () => /MicroMessenger/i.test(window.navigator.userAgent);
@DefectingCat
DefectingCat / update.js
Last active April 21, 2022 07:44
数组去重合并
let arr = [
{ name: 'zs', age: 15, happy: ['唱歌', '玩游戏'] },
{ name: 'ls', age: 15, happy: ['玩游戏'] },
{ name: 'zs', age: 15, happy: ['学习', '划水'] },
];
function test(arr) {
let _obj = {};
let _arr = [];
if (!arr.length) return;
@DefectingCat
DefectingCat / TypeScript Debounce.ts
Last active April 21, 2022 07:48
TypeScript Debounce
// "typescript": "~4.1.5"
type Debounce = {
<T extends unknown[]>(fn: (...arg: T) => void | unknown, ms: number): (
this: unknown,
...arg: T
) => void | unknown;
};
/**
* debounce function wrapper
@DefectingCat
DefectingCat / class_super.js
Last active April 21, 2022 08:02
This is JavaScript
class ParentA {
constructor() {
this.id = 'a';
}
foo() {
console.log('Parent A: ', this.id);
}
}
class ParentB {
@DefectingCat
DefectingCat / generator-fibonacci.ts
Last active April 21, 2022 08:03
Fibonacci with generator
function* fibonacciSquence() {
let x = 0,
y = 1;
for (;;) {
yield y;
[x, y] = [y, x + y];
}
}
const fibonacci = (n: number) => {
@DefectingCat
DefectingCat / fibonacci.js
Last active April 21, 2022 08:06
Fibonacci
function fibonacci(num) {
let count = 0;
let result = [1, 1];
if (num <= 1) {
return result;
}
let n = num - 2;
for (let i = 0; i < n; i++) {
result.push(result[count] + result[count + 1]);
count++;
@DefectingCat
DefectingCat / 访问器属性与私有化变量.js
Last active April 21, 2022 08:06
访问器属性与私有化变量
let xfy = (() => {
let name = 'xfy';
let age = 18;
let obj = {};
Object.defineProperty(obj, 'test', {
get: function () {
return name + ' : ' + age;
},
set: function (value) {
name = value;
let obj = {
a: "xfy",
b: {
c: "test",
},
d: 123,
e: [1, 2, 3, 4, 5],
f: {
g: 'array',
h: [1, 2, 3, 4, 5, 6, {