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

JS中的一些技巧 #7

Open
liandmin opened this issue May 21, 2018 · 0 comments
Open

JS中的一些技巧 #7

liandmin opened this issue May 21, 2018 · 0 comments

Comments

@liandmin
Copy link
Owner

###1. 使用!!将变量转成布尔类型

除了false0undefinednull''NaN返回false,其他的都返回true

2. 使用+将变量转换为数字

注意这个规则只适用于数字字符串,其他的会返回NaN+new Date()会返回时间戳

console.log( + '123')  //123
console.log( + '0Abc')  //NaN
console.log( + new Date())  //时间戳

3. 短路条件&&

a && b 表示如果a为真,则执行b;可以用这种方法检查对象是否存在某些属性或者函数

let user = {
	name: 'lx'
}

console.log(user && user.name)

4. 使用||设置默认值

ES6的默认参数可能使用||实现的。a = x || 1 表示如果x为false,则a为1

5. 检查对象中是否存在某个属性:in

if ('name' in window) {
	console.log('name is exist')
} else {
	console.log('no')
}

6. 把NodeList转换成数组

  1. 通过[].slice.call(element)
  2. 通过ES6新增的Array.from()方法
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant