Skip to content

Instantly share code, notes, and snippets.

View xuhen's full-sized avatar
💭
I may be slow to respond.

Edward xu xuhen

💭
I may be slow to respond.
View GitHub Profile
1. https://media.licdn.cn/dms/image/C4D12AQFNtlOBwfGrPQ/article-cover_image-shrink_600_2000/0/1582472887635?e=1639612800&v=beta&t=8carkRftCwb-8ZTXe8ZSFh3ewfWRPd2ZrHVgJ8SD9Jg
function permutation(string) {
if (string.length < 2) return string;
var permutations = [];
for (var i = 0; i < string.length; i++) {
var char = string[i];
/**
* Pushes the items of `iterable` into `sink`, a generator.
* It uses the generator method `next()` to do so.
*/
function send(iterable: Iterable<any>, sink: any, config?: { log: boolean }) {
for (const x of iterable) {
sink.next(x);
if (config && config.log) {
console.log('===>', x);
/**
* Returns an iterable that transforms the input sequence
* of characters into an output sequence of words.
*/
function* tokenize(chars: string) {
const iterator = chars[Symbol.iterator]();
let ch;
do {
ch = getNextItem(iterator); // (A)
// 将form序列化为key1=value1&key2=value2这样的个数,在ajax提交form表单时很有用。
function serialize(form) {
var parts = [],
field = null, i,
len,
j, optLen, option, optValue;
for (i = 0, len = form.elements.length; i < len; i++) {
field = form.elements[i];
switch (field.type) {
// 将浏览器的location上的search字段转换成对象,方便获取值
// ex: ?name=edward&age=34 => {name: 'edward', age: 34}
function getQueryStringArgs() {
var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
args = {},
items = qs.length ? qs.split("&") : [],
item = null,
name = null,
value = null,
// 记忆函数,为了更少的计算,以空间换时间。
var memoize = function (func, hasher) {
var memoize = function (key) {
var cache = memoize.cache;
var address = '' + (hasher ? hasher.apply(this, arguments) : key);
console.log('address', address, key)
if (!cache[address]) {
cache[address] = func.apply(this, arguments);
}
return cache[address];
// 代理实现拖拽功能,并且用自定义事件让触发拖拽逻辑和处理拖拽解耦。
// html
<div id="status"></div>
<div id="box" class="draggable" style="position:absolute; background:red;width: 200px;height: 200px;"> </div>
// js
function EventTarget() {
this.handlers = {};
// html
<select name="title" id="title" style="width:152px;">
<option value="1">十进制转二进制</option>
<option value="2">十进制转八进制</option>
<option value="3">十进制转十六进制</option>
<option value="4">二进制转十进制</option>
<option value="5">八进制转十进制</option>
<option value="6">十六进制转十进制</option>
<option value="7">二进制转八进制</option>
<option value="8">八进制转二进制</option>
// html
<span id="clock"></span>
// js
(function () {
const endTime = new Date().getTime() + 10 * 24 * 60 * 60 * 1000;
const clock = document.getElementById('clock');
// ECMA used the name Symbol.iterator. Symbols offer names that are unique and cannot clash
// with other property names. Also, Symbol.iterator will return an object called an iterator.
// This iterator will have a method called next which will return an object with keys value and done.
const iterable = {
[Symbol.iterator]() {
let step = 0;
const iterator = {
next() {