Skip to content

Commit f6f92fb

Browse files
author
sunhui04
committed
update
1 parent f72a8f6 commit f6f92fb

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,34 @@
44

55
经验不足,能力有限,还望轻拍。
66

7+
####2016-01-11:判断对象中是否存在某个属性
8+
9+
我们经常需要用到对象中的某个属性,为了保证程序的健壮性,在使用之前我们需要判断这个属性是否存在,可以用`if`来判断
10+
11+
if(obj[property] !== undefined){
12+
// do something
13+
}
14+
15+
更方便的方式是使用这两个方法:`hasOwnProperty``in`
16+
17+
function Person (name) {
18+
this.name = name;
19+
}
20+
Person.prototype.age = '22';
21+
22+
var person = new Person("sunhui");
23+
console.log(person.hasOwnProperty('name')); // true
24+
console.log("name" in person); // true
25+
26+
console.log(person.hasOwnProperty('age')); // false
27+
console.log("age" in person); // true
28+
29+
30+
`hasOwnProperty``in`都可以用来判断某个属性是否存在于对象中,区别就是`hasOwnProperty`不能搜索到从原型链继承的属性,而`in`可以。
31+
32+
关键词:**hasOwnProperty****in****原型链**
33+
34+
735
####2016-01-10:对象数组根据某个属性排序
836

937
function compare(prop){

0 commit comments

Comments
 (0)