Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

函数强大问题 #1

Open
scscms opened this issue Jul 25, 2019 · 2 comments
Open

函数强大问题 #1

scscms opened this issue Jul 25, 2019 · 2 comments

Comments

@scscms
Copy link

scscms commented Jul 25, 2019

/**
 18、capitalize:字符串首位大写
 */
function capitalize(str){
	return str.charAt(0).toUpperCase() + str.slice(1)
}
capitalize('aA')
capitalize(null)
capitalize(undefined)
capitalize(NaN)

....需要优化强大

@scscms
Copy link
Author

scscms commented Jul 25, 2019

/*
 16、camelize:横线转驼峰命名
 * */
let camelizeRE = /-(\w)/g;
function camelize(str) {
	return str.replace(camelizeRE, function(_, c) {
		return c ? c.toUpperCase() : '';
	})
}

1.库最好不要有私有变量
2.return c ? c.toUpperCase() : ''; 这里不需要判断,直接return c.toUpperCase()

@scscms
Copy link
Author

scscms commented Jul 25, 2019

/*
 45、arr.findIndex:返回数组中通过测试(函数fn内判断)的第一个元素的下标
 * */
Array.prototype.findIndex = Array.prototype.findIndex || function findIndex(fn, ctx){
	fn = fn.bind(ctx)
	
	let result;
	this.some((value, index, arr) => {
		return fn(value, index, arr) ? (result = index, true) : false
	})
	
	return result
}

这里就很矛盾了:都可以使用Array.some怎么就不支持Array.findIndex呢?还支持ES6箭头函数的浏览器也会缺少findIndex支持?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant