File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 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){
You can’t perform that action at this time.
0 commit comments