This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 将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) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 将浏览器的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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 记忆函数,为了更少的计算,以空间换时间。 | |
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]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 代理实现拖拽功能,并且用自定义事件让触发拖拽逻辑和处理拖拽解耦。 | |
// 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 = {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// html | |
<span id="clock"></span> | |
// js | |
(function () { | |
const endTime = new Date().getTime() + 10 * 24 * 60 * 60 * 1000; | |
const clock = document.getElementById('clock'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() { |
NewerOlder