Code Monkey home page Code Monkey logo

Comments (44)

huyansheng3 avatar huyansheng3 commented on May 22, 2024 9

完全是 js 的糟粕吧。。。。各种隐式的代码背后的逻辑说不定那个版本就改了。。老老实实用官方 api 不好吗?

from blog.

zhangbuding avatar zhangbuding commented on May 22, 2024 5

无聊翻着看,看到说的 xx 邀请码,直接公司大屏幕搜了下....之前真不知道1024是个啥

from blog.

unix avatar unix commented on May 22, 2024 2

@IMLaoJI
typeof item + item 就是设置一个键,键名为值的类型 + 值。
然后查找这个缓存的对象里面有没有相同的键就知道这个值是不是遍历过了。

from blog.

axuebin avatar axuebin commented on May 22, 2024 1

@jawil 还得感谢GayHub啊... 一眼就看出来了...

from blog.

 avatar commented on May 22, 2024 1

@xiyuyizhi 其实可以再深入一些,可以考虑带小数的情况

function formatNumber(num) {
    return String(num).replace(/\d(?=(\d{3})+(\.\d+)?$)/g, '$&,');
}

from blog.

yanche avatar yanche commented on May 22, 2024 1

看到没人提,就提一下
var args = [...arguments]; 对于arguments对象是可以的
但是对一般的 {0:"a", 1:"b", length: 2} 这种是不行的,Array.from 都可以

from blog.

lynxerzhang avatar lynxerzhang commented on May 22, 2024 1

空数组导致reduce报错那个,看文档是因为

initialValue
用作第一个调用 callback的第一个参数的值。 如果没有提供初始值,则将使用数组中的第一个元素。 在没有初始值的空数组上调用 reduce 将报错。

所以initialValue不要空着,加上0就行了

let arr = []
function sum(arr) {
    return arr.reduce((a, b) => a + b, 0)
}
sum(arr) //0

这样就不会报错了

from blog.

jawil avatar jawil commented on May 22, 2024

圣诞快乐,1024邀请码,有缘人得!
a6e1bbfa4e2e4bf7 | 2017-12-23 17:50:04 |   |   | 未使用 | 邀请

c7a5a23f4f0e5cc7 | 2017-12-23 17:50:04 |   |   | 未使用 | 邀请

from blog.

lidongjies avatar lidongjies commented on May 22, 2024

这是啥

from blog.

jawil avatar jawil commented on May 22, 2024

不可能改的,JavaScript 设计者都承认 null 是个对象都是一种错误的决定,也没看改过来,原因你懂得。
为了向下兼容,这些东西都是 ECMAScript 的东西,怎么能说不定就改了,对于 JavaScript 这种脚本语言,只可能拓展新的功能,不可能改变原来的功能的。

还有就是糟粕问题,完全不敢苟同,我觉得是玄学。@huyansheng3

from blog.

axuebin avatar axuebin commented on May 22, 2024

数组去重那部分小标题权重错了。。

from blog.

jawil avatar jawil commented on May 22, 2024

感谢指正,哈哈,这些细节都被看出来了 @axuebin

from blog.

xinkule avatar xinkule commented on May 22, 2024

大佬真的很会玩。。。最容易懂的才是好方法呀

from blog.

LingZhenhua avatar LingZhenhua commented on May 22, 2024

1.2 Promise 版本运行报错是为何
//我又仔细看了看,没有错,在chrome里的console我直接粘贴进去没加分号,尴尬,不过直接在node.js运行的话,就没问题
`function sleep(time) {
return new Promise(resolve => setTimeout(resolve, time))
};

const t1 = +new Date();
sleep(3000).then(() => {
const t2 = +new Date();
console.log(t2 - t1);
})`
//如果没加分号,在chrome里的报错是 Uncaught SyntaxError: Identifier 't1' has already been declared
// at :1:1

from blog.

jawil avatar jawil commented on May 22, 2024

有报错截图吗?请在 Chrome 浏览器运行或者在 node 高版本环境运行@LingZhenhua

from blog.

xanke avatar xanke commented on May 22, 2024

获取时间戳还可以 Date.now()

from blog.

jawil avatar jawil commented on May 22, 2024

哈哈,把这个竟然忘了,不错,谢谢补充~ @xanke

from blog.

limoning avatar limoning commented on May 22, 2024

貌似 +new Date 也一样可以获取的

from blog.

jawil avatar jawil commented on May 22, 2024

不带参数就是当前时间的时间戳,确实可以 @limoning

from blog.

GitHubJiKe avatar GitHubJiKe commented on May 22, 2024

这些玄学,写在代码里,懂的人,会心一笑,不懂得人,能懵一脸血!

from blog.

unix avatar unix commented on May 22, 2024

typo: '便利器接口' => '遍历器接口 (Iterator)'

from blog.

GONDARJJC avatar GONDARJJC commented on May 22, 2024

数字格式化那里可以用reduceRight,就不用reverse了。
学习到了好多新知识,感谢分享

from blog.

IMLaoJI avatar IMLaoJI commented on May 22, 2024

return obj.hasOwnProperty(typeof item + item) ?
false :
(obj[typeof item + item] = true)
这咋理解。。。

from blog.

FrontToEnd avatar FrontToEnd commented on May 22, 2024

4.4 API版的例子:

(123456789).toLocaleString('en-US')   //值应该是"123,456,789"

from blog.

erniu avatar erniu commented on May 22, 2024

var a = [1, 1, '1', '2', 1]
function unique(arr) {
var res = []
for (var i = 0, len = arr.length; i < len; i++) {
var item = arr[i]
for (var j = 0, len = res.length; j < jlen; j++) {
if (item === res[j]) //arr数组的item在res已经存在,就跳出循环
break
}
if (j === jlen) //循环完毕,arr数组的item在res找不到,就push到res数组中
res.push(item)
}
return res
}
console.log(unique(a)) // [1, 2, "1"]

3.1 第二个循环的长度变量手误了,23333

from blog.

IMLaoJI avatar IMLaoJI commented on May 22, 2024

@WittBulter 好棒 谢谢你!

from blog.

lynxerzhang avatar lynxerzhang commented on May 22, 2024

想请教下,在讲述Array.slice那节里面,“因为 ie 下的 dom 对象是以 com 对象的形式实现的,js 对象与com对象不能进行转换 ”,com对象是指什么?

from blog.

jawil avatar jawil commented on May 22, 2024

两种不同的标准,早期浏览器战争时代,各自都执行各自的标准,那时候标准都不统一。
HTML DOM 是 W3C 标准(是 HTML 文档对象模型的英文缩写,Document Object Model for HTML)。
HTML DOM 定义了用于 HTML 的一系列标准的对象,以及访问和处理 HTML 文档的标准方法。
通过 DOM,可以访问所有的 HTML 元素,连同它们所包含的文本和属性。可以对其中的内容进行修改和删除,同时也可以创建新的元素。
HTML DOM 独立于平台和编程语言。它可被任何编程语言诸如 Java、JavaScript 和 VBScript 使用。
COM组件是遵循COM规范编写、以Win32动态链接库(DLL)或可执行文件(EXE)形式发布的可执行二进制代码,能够满足对组件架构的所有需求。遵循COM的规范标准,组件与应用、组件与组件之间可以互操作,极其方便地建立可伸缩的应用系统。COM是一种技术标准,其商业品牌则称为ActiveX。
@lynxerzhang

from blog.

scplay avatar scplay commented on May 22, 2024

�toLocalString 居然不能转成大写的中文,一点都不好玩

from blog.

lynxerzhang avatar lynxerzhang commented on May 22, 2024

@jawil 感谢回复,谢谢!

from blog.

zoffyzhang avatar zoffyzhang commented on May 22, 2024

后端的人也很少写位操作运算吧,好像只有搞过算法竞赛的人才喜欢写

from blog.

IMLaoJI avatar IMLaoJI commented on May 22, 2024

from blog.

ayou-ok avatar ayou-ok commented on May 22, 2024

谢谢分享

from blog.

xiyuyizhi avatar xiyuyizhi commented on May 22, 2024

对于数字字符串转千分位的分析有些异议

function formatNumber(str) {
  return str.replace(/\B(?=(\d{3})+(?!\d))/g, ',')
}

console.log(formatNumber("123456789")) // 1,234,567,890
  1. \B是 不是单词边界,也就是除了'1'前面的位置,1和2之间,2和3之间,3和4之间等

  2. (?=(\d{3})+(?!\d))的作用就是说明,除了1之前的位置,其他字符之间的边界,后面必须跟着3N个数字直到字符串末尾,把(?!\d)换成$更好。/\B(?=(\d{3})+$)/g

from blog.

liuqipeng417 avatar liuqipeng417 commented on May 22, 2024

image
此处是否有问题,Date 实例走 ToPrimitive 优先走 toString?

from blog.

lyh2668 avatar lyh2668 commented on May 22, 2024

很多都很有用的,不过对于+new Date()这块一时还不是很明白。
我也不喜欢加分号,不过写交换变量的时候的确存在问题...那就把分号加前面吧...
;[a, b] = [b, a]

from blog.

haishenming avatar haishenming commented on May 22, 2024

github还能这样用......

from blog.

hua1995116 avatar hua1995116 commented on May 22, 2024

交换顺序那个,我还看到过
var a = 2,b =3; b = [a, a=b][0]

from blog.

Dcatfly avatar Dcatfly commented on May 22, 2024

@hua1995116 前几天好像还看到小胡子哥在知乎上写过这个?

from blog.

plh97 avatar plh97 commented on May 22, 2024

真的是大神

from blog.

hooper-hc avatar hooper-hc commented on May 22, 2024

reduce 有风险, 如果数组是空数组 [], 会爆错老哥

from blog.

xieww avatar xieww commented on May 22, 2024

总结挺到位。

from blog.

forzalianjunting avatar forzalianjunting commented on May 22, 2024

@yanche 那是因为扩展运算符会默认调用 Iterator 接口,而 arguments 原生具备 Iterator 接口

from blog.

zhaoleipeng avatar zhaoleipeng commented on May 22, 2024

亲嘴的那个动图咋弄出来的撒。给个调用方法。

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.