风马牛不相及
风马牛不相及
fix chinese guide typesetting
一些不错的js题
## 输出内容系列 > 输出什么? ```js function getPersonInfo(one, two, three) { console.log(one); console.log(two); console.log(three); } const person = "Lydia"; const age = 21; getPersonInfo`${person} is ${age} years old`; ``` 是不是大吃一惊,哈哈。这个细节还真是没有注意到。
惰性函数
今天看到一篇文章中有提到 **惰性函数** 。感觉很新奇,去查了下,发现就是自己调用自己。 可以让你只在第一次的时候调用你需要处理的函数,后面可以直接使用函数。 ```js var foo = function() { console.log('你会发现我只出现一次哦,不管你调用几次') foo = function() { console.log('嘻嘻,我出现了哦') } foo() } foo() // 你会发现我只出现一次哦, 不管你调用几次; 嘻嘻,我出现了哦 foo() //嘻嘻,我出现了哦 ``` 还挺有意思的,留下个问题,你觉得这个是因为啥,为啥会出现上面的情况?
这里记录一些you don't know js提出的一些小细节,小技巧。 ## 章: types&&grammer [点击查看](https://github.com/xiaohesong/TIL/tree/master/front-end/javascript/you-dont-known-js/types%26grammer)此章各小节 ### values - isNaN 测试传入的东西要么不是number,要么是number。但是这样是严谨的吗? ```js var a = 2 / "foo"; var b = "foo"; a; // NaN b; // "foo"...
今天无意间看到一个面试题有问到如何实现一个`reduce`函数,额,花了点时间算是写下来了。写这个方法的前提是了解这个api。 - 默认初始值 这个就是对应的`reduce`的第二个参数。 - 如果有这个参数,那么这个参数将作为`reduce`的第一个参数(函数)的第一个参数,(函数)的第二个参数是数组的第一个参数; - 如果没有这个,那么`reduce`函数的第一个参数(函数)的第一个参数是数组的第一个元素,(函数)的第二个参数就是数组的第二个元素。 所以,`reduce`函数的第一个参数(函数)的第三个参数(索引), 就是根据`reduce`函数的第二个参数在数组中的牵引做的判断。 好了,我们知道了这个`reduce`函数的`api`之后,我们尝试来写个: ```js const array = [11, 22, 33] const reducer = (arr, fn, initValue) => { if(!Array.isArray(arr)) throw new Error('First...
今天看到一个地方有误解,自己试了下,感觉挺不错。 你觉得下面的这个`console`的`saga`会输出什么? ```js function run({name, age}, saga, ...options){ console.log('name is', name, 'saga is', saga) } let boundRun = run.bind(null, {name: 'xiaohesong', age: 18}) function *rootSaga() { yield 'i am a...
```html 测试 Name: add: + let input = document.querySelector('.input') let button = document.querySelector('.button') let num = 0 button.addEventListener('click', function(){ num++ input.value = num }) let proxy = new Proxy(input, {...
之前有写过一篇文章,是说[柯里化函数和函数组合](https://github.com/xiaohesong/TIL/blob/master/front-end/javascript/higher-order-function/curry.md), 看完之后真是大受脾益,尤其是在看`redux`源码的时候,就感觉很顺通。 今天上午看了下简书,发现了一个被面试到的问题,思考了下。问题是这样的: > 函数闭包与柯里化(让手写一个函数完成求和操作,func(1)(2)(3)、func(1,2)(3)和func(1,2,3)都能保证可以正常求和) ```js function add(...args) { return args.reduce((total, item) => total = total + item, 0) } function func(fn){ return (length) => (...args) => (length - args.length)...
## 翻转字符串 [How to reverse a string in JavaScript](https://codeburst.io/how-to-reverse-a-string-in-javascript-4ec835202243) 1. ```js const str = "hello" str.split('').reverse().join('') //"olleh" ``` 2. ```js const str = "hello" [...str].reduce((prev,next)=>next+prev) //"olleh" ``` 3. ```js function...