|
4 | 4 |
|
5 | 5 | 经验不足,能力有限,还望轻拍。 |
6 | 6 |
|
| 7 | +####2016-01-13:检测数据类型 |
| 8 | + |
| 9 | +我们通常会使用`typeof`、`instanceof`、`isArray`来检测数据的类型,这里我介绍一种万能型的:调用`Object`的`toString`方法。 |
| 10 | + |
| 11 | + function a(){console.log("yes");} |
| 12 | + console.log(Object.prototype.toString.call(a)); // [object Function] |
| 13 | + var b = 123; |
| 14 | + console.log(Object.prototype.toString.call(b)); // [object Number] |
| 15 | + var c = "sunhui"; |
| 16 | + console.log(Object.prototype.toString.call(c)); // [object String] |
| 17 | + var d = true; |
| 18 | + console.log(Object.prototype.toString.call(d)); // [object Boolean] |
| 19 | + var e = [1,2,3]; |
| 20 | + console.log(Object.prototype.toString.call(e)); // [object Array] |
| 21 | + var f; |
| 22 | + console.log(Object.prototype.toString.call(f)); // [object Undefined] |
| 23 | + var g = null; |
| 24 | + console.log(Object.prototype.toString.call(g)); // [object Null] |
| 25 | + var h = {"name":"sunyuhui"}; |
| 26 | + console.log(Object.prototype.toString.call(h)); // [object Object] |
| 27 | + |
| 28 | +使用`call`调用`Object`的`toString`方法,将会返回一个遵循[object NativeConstructorName]格式的字符串。其中NativeConstructorName指的就是变量的构造函数名。 |
| 29 | + |
| 30 | +####2016-01-12: 变量和函数的提前声明 |
| 31 | + |
| 32 | +在JavaScript里 **变量声明** 是让系统知道这个变量存在,而**变量定义**是给这个变量赋值。**变量声明**会被提前到顶部,而 **变量定义** 不会。 |
| 33 | + |
| 34 | + console.log(a); // "ReferenceError: a is not defined" |
| 35 | + |
| 36 | +上面的代码报变量没有定义的错误。 |
| 37 | + |
| 38 | + console.log(a); // undefined |
| 39 | + var a; |
| 40 | + |
| 41 | +上面的代码提示`undefined`,这说明我们对`a`的声明被提前了。再来看: |
| 42 | + |
| 43 | + console.log(a); // undefined |
| 44 | + var a = 3; |
| 45 | + |
| 46 | +我们在这里对`a`同时做了**声明**和**定义**两个操作,从结果来看,只有**声明**被提前了,而**定义**却没有。 |
| 47 | + |
| 48 | +接下来看函数: |
| 49 | + |
| 50 | + getName(); // "sunyuhui" |
| 51 | + function getName(){ |
| 52 | + console.log("sunyuhui"); |
| 53 | + } |
| 54 | + |
| 55 | +结果说明我们对函数的声明被提前了。 |
| 56 | + |
| 57 | + getAge(); // "TypeError: getAge is not a function" |
| 58 | + var getAge = function(){ |
| 59 | + console.log(22); |
| 60 | + } |
| 61 | + |
| 62 | +事实上,第一种方式叫**函数声明**,这种方式能被提前到顶部,第二种方式叫**函数表达式**,不会被提前。 |
| 63 | + |
| 64 | +关键词:**声明**、 **提前** |
| 65 | + |
| 66 | + |
| 67 | +####2016-01-11:判断对象中是否存在某个属性 |
| 68 | + |
| 69 | +我们经常需要用到对象中的某个属性,为了保证程序的健壮性,在使用之前我们需要判断这个属性是否存在,可以用`if`来判断 |
| 70 | + |
| 71 | + if(obj[property] !== undefined){ |
| 72 | + // do something |
| 73 | + } |
| 74 | + |
| 75 | +更方便的方式是使用这两个方法:`hasOwnProperty`和`in`。 |
| 76 | + |
| 77 | + function Person (name) { |
| 78 | + this.name = name; |
| 79 | + } |
| 80 | + Person.prototype.age = '22'; |
| 81 | + |
| 82 | + var person = new Person("sunhui"); |
| 83 | + console.log(person.hasOwnProperty('name')); // true |
| 84 | + console.log("name" in person); // true |
| 85 | + |
| 86 | + console.log(person.hasOwnProperty('age')); // false |
| 87 | + console.log("age" in person); // true |
| 88 | + |
| 89 | + |
| 90 | +`hasOwnProperty`和`in`都可以用来判断某个属性是否存在于对象中,区别就是`hasOwnProperty`不能搜索到从原型链继承的属性,而`in`可以。 |
| 91 | + |
| 92 | +关键词:**hasOwnProperty**、**in**、**原型链** |
| 93 | + |
| 94 | + |
7 | 95 | ####2016-01-10:对象数组根据某个属性排序 |
8 | 96 |
|
9 | 97 | function compare(prop){ |
|
0 commit comments