hasOwnProperty

hasOwnProperty 方法会返回一个布尔值,指示对象自身属性中是否具有指定的属性(也就是,是否有指定的键)

所有继承了 Object 的对象都会继承到 hasOwnProperty 方法。这个方法可以用来检测一个对象是否含有特定的自身属性;和 in 运算符不同,该方法会忽略掉那些从原型链上继承到的属性

即使属性的值是 nullundefined,只要属性存在,hasOwnProperty 依旧会返回 true

参数说明

  1. hasOwnProperty (
  2. prop: string | Symbol,
  3. ) return boolean // 用来判断某个对象是否含有指定的属性的布尔值
参数名 是否必填 类型 说明
prop string 要检测的属性的 [String](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String)字符串形式表示的名称,或者 Symbol

示例

  1. o = new Object();
  2. o.hasOwnProperty('prop'); // false
  3. o.prop = 'exists';
  4. o.hasOwnProperty('prop'); // true
  5. delete o.prop;
  6. o.hasOwnProperty('prop'); // false

判断 undefinednull
  1. o = new Object();
  2. o.propOne = null;
  3. o.hasOwnProperty('propOne'); // true
  4. o.propTwo = undefined;
  5. o.hasOwnProperty('propTwo'); // true

prototype 上的属性不做数
  1. o = new Object();
  2. o.prop = 'exists';
  3. o.hasOwnProperty('prop'); // 返回 true
  4. o.hasOwnProperty('toString'); // 返回 false
  5. o.hasOwnProperty('hasOwnProperty'); // 返回 false

isPrototypeOf

isPrototypeOf 方法用于测试一个对象是否存在于另一个对象的原型链上。

参数说明

  1. isPrototypeOf (
  2. obj: object
  3. ) return boolean // 表示调用对象是否在另一个对象的原型链上
参数名 是否必填 类型 说明
obj object 在该对象的原型链上搜寻

示例

  1. function Foo() {}
  2. function Bar() {}
  3. function Baz() {}
  4. Bar.prototype = Object.create(Foo.prototype);
  5. Baz.prototype = Object.create(Bar.prototype);
  6. var baz = new Baz();
  7. console.log(Baz.prototype.isPrototypeOf(baz)); // true
  8. console.log(Bar.prototype.isPrototypeOf(baz)); // true
  9. console.log(Foo.prototype.isPrototypeOf(baz)); // true
  10. console.log(Object.prototype.isPrototypeOf(baz)); // true

propertyIsEnumerable

propertyIsEnumerable 方法返回一个布尔值,表示指定的属性是否可枚举。

每个对象都有一个 propertyIsEnumerable 方法。此方法可以确定对象中指定的属性是否可以被 for in 循环枚举,但是通过原型链继承的属性除外。如果对象没有指定的属性,则此方法返回 false

参数说明

  1. propertyIsEnumerable (
  2. prop: string
  3. ) return boolean // 用来表示指定的属性名是否可枚举的 boolean
参数名 是否必填 类型 说明
prop string 需要测试的属性名

示例

  1. var o = {};
  2. var a = [];
  3. o.enumerable = 'is enumerable';
  4. a[0] = 'is enumerable';
  5. Object.defineProperty(o, 'notEnumerable',
  6. {
  7. value: 'not enumerable'
  8. }
  9. )
  10. o.propertyIsEnumerable('prop'); // true
  11. a.propertyIsEnumerable(0); // true
  12. o.propertyIsEnumerable('notEnumerable') // false
  13. a.propertyIsEnumerable('length') // false

toString

toString 方法返回一个表示该对象的字符串

参数说明

  1. toString () return string // 一个表示该对象的字符串

示例

  1. var o = new Object();
  2. o.toString(); // 返回 [object Object]

妙用

我们可以使用 Object.prototype.toString 来区分任意类型
  1. var toString = Object.prototype.toString;
  2. toString.call(new Date); // [object Date]
  3. toString.call(new String); // [object String]
  4. toString.call(Math); // [object Math]
  5. toString.call(undefined); // [object Undefined]
  6. toString.call(null); // [object Null]

valueOf

valueOf 方法返回指定对象的原始值

参数说明

  1. valueOf () return any // 返回值为该对象的原始值

示例

多类型使用

所有类型都自己独特的 valueOf 方法,这里我们一同展示了

  1. // Array:返回数组对象本身
  2. var array = ["ABC", true, 12, -5];
  3. console.log(array.valueOf() === array); // true
  4. // Date:当前时间距 1970 年 1 月 1 日午夜的毫秒数
  5. var date = new Date(2013, 7, 18, 23, 11, 59, 230);
  6. console.log(date.valueOf()); // 1376838719230
  7. // Number:返回数字值
  8. var num = 15.26540;
  9. console.log(num.valueOf()); // 15.2654
  10. // 布尔:返回布尔值 true 或 false
  11. var bool = true;
  12. console.log(bool.valueOf() === bool); // true
  13. // new 一个 Boolean 对象
  14. var newBool = new Boolean(true);
  15. // valueOf() 返回的是 true,两者的值相等
  16. console.log(newBool.valueOf() == newBool); // true
  17. // 但是不全等,两者类型不相等,前者是 boolean 类型,后者是 object 类型
  18. console.log(newBool.valueOf() === newBool); // false
  19. // Function:返回函数本身
  20. function foo(){}
  21. console.log( foo.valueOf() === foo ); // true
  22. var foo2 = new Function("x", "y", "return x + y;");
  23. console.log( foo2.valueOf() );
  24. /*
  25. ƒ anonymous(x,y
  26. ) {
  27. return x + y;
  28. }
  29. */
  30. // Object:返回对象本身
  31. var obj = {name: "张三", age: 18};
  32. console.log( obj.valueOf() === obj ); // true
  33. // String:返回字符串值
  34. var str = "http://www.xyz.com";
  35. console.log( str.valueOf() === str ); // true
  36. // new 一个字符串对象
  37. var str2 = new String("http://www.xyz.com");
  38. // 两者的值相等,但不全等,因为类型不同,前者为 string 类型,后者为 object 类型
  39. console.log( str2.valueOf() === str2 ); // false