hasOwnProperty
hasOwnProperty 方法会返回一个布尔值,指示对象自身属性中是否具有指定的属性(也就是,是否有指定的键)
所有继承了 Object 的对象都会继承到 hasOwnProperty 方法。这个方法可以用来检测一个对象是否含有特定的自身属性;和 in 运算符不同,该方法会忽略掉那些从原型链上继承到的属性
即使属性的值是 null 或 undefined,只要属性存在,hasOwnProperty 依旧会返回 true
参数说明
hasOwnProperty (prop: string | Symbol,) return boolean // 用来判断某个对象是否含有指定的属性的布尔值
| 参数名 | 是否必填 | 类型 | 说明 |
|---|---|---|---|
| prop | 是 | string | 要检测的属性的 [String](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String)字符串形式表示的名称,或者 Symbol |
示例
o = new Object();o.hasOwnProperty('prop'); // falseo.prop = 'exists';o.hasOwnProperty('prop'); // truedelete o.prop;o.hasOwnProperty('prop'); // false
判断 undefined 和 null
o = new Object();o.propOne = null;o.hasOwnProperty('propOne'); // trueo.propTwo = undefined;o.hasOwnProperty('propTwo'); // true
prototype 上的属性不做数
o = new Object();o.prop = 'exists';o.hasOwnProperty('prop'); // 返回 trueo.hasOwnProperty('toString'); // 返回 falseo.hasOwnProperty('hasOwnProperty'); // 返回 false
isPrototypeOf
isPrototypeOf 方法用于测试一个对象是否存在于另一个对象的原型链上。
参数说明
isPrototypeOf (obj: object) return boolean // 表示调用对象是否在另一个对象的原型链上
| 参数名 | 是否必填 | 类型 | 说明 |
|---|---|---|---|
| obj | 是 | object | 在该对象的原型链上搜寻 |
示例
function Foo() {}function Bar() {}function Baz() {}Bar.prototype = Object.create(Foo.prototype);Baz.prototype = Object.create(Bar.prototype);var baz = new Baz();console.log(Baz.prototype.isPrototypeOf(baz)); // trueconsole.log(Bar.prototype.isPrototypeOf(baz)); // trueconsole.log(Foo.prototype.isPrototypeOf(baz)); // trueconsole.log(Object.prototype.isPrototypeOf(baz)); // true
propertyIsEnumerable
propertyIsEnumerable 方法返回一个布尔值,表示指定的属性是否可枚举。
每个对象都有一个 propertyIsEnumerable 方法。此方法可以确定对象中指定的属性是否可以被 for in 循环枚举,但是通过原型链继承的属性除外。如果对象没有指定的属性,则此方法返回 false
参数说明
propertyIsEnumerable (prop: string) return boolean // 用来表示指定的属性名是否可枚举的 boolean
| 参数名 | 是否必填 | 类型 | 说明 |
|---|---|---|---|
| prop | 是 | string | 需要测试的属性名 |
示例
var o = {};var a = [];o.enumerable = 'is enumerable';a[0] = 'is enumerable';Object.defineProperty(o, 'notEnumerable',{value: 'not enumerable'})o.propertyIsEnumerable('prop'); // truea.propertyIsEnumerable(0); // trueo.propertyIsEnumerable('notEnumerable') // falsea.propertyIsEnumerable('length') // false
toString
toString 方法返回一个表示该对象的字符串
参数说明
toString () return string // 一个表示该对象的字符串
示例
var o = new Object();o.toString(); // 返回 [object Object]
妙用
我们可以使用 Object.prototype.toString 来区分任意类型
var toString = Object.prototype.toString;toString.call(new Date); // [object Date]toString.call(new String); // [object String]toString.call(Math); // [object Math]toString.call(undefined); // [object Undefined]toString.call(null); // [object Null]
valueOf
valueOf 方法返回指定对象的原始值
参数说明
valueOf () return any // 返回值为该对象的原始值
示例
多类型使用
所有类型都自己独特的 valueOf 方法,这里我们一同展示了
// Array:返回数组对象本身var array = ["ABC", true, 12, -5];console.log(array.valueOf() === array); // true// Date:当前时间距 1970 年 1 月 1 日午夜的毫秒数var date = new Date(2013, 7, 18, 23, 11, 59, 230);console.log(date.valueOf()); // 1376838719230// Number:返回数字值var num = 15.26540;console.log(num.valueOf()); // 15.2654// 布尔:返回布尔值 true 或 falsevar bool = true;console.log(bool.valueOf() === bool); // true// new 一个 Boolean 对象var newBool = new Boolean(true);// valueOf() 返回的是 true,两者的值相等console.log(newBool.valueOf() == newBool); // true// 但是不全等,两者类型不相等,前者是 boolean 类型,后者是 object 类型console.log(newBool.valueOf() === newBool); // false// Function:返回函数本身function foo(){}console.log( foo.valueOf() === foo ); // truevar foo2 = new Function("x", "y", "return x + y;");console.log( foo2.valueOf() );/*ƒ anonymous(x,y) {return x + y;}*/// Object:返回对象本身var obj = {name: "张三", age: 18};console.log( obj.valueOf() === obj ); // true// String:返回字符串值var str = "http://www.xyz.com";console.log( str.valueOf() === str ); // true// new 一个字符串对象var str2 = new String("http://www.xyz.com");// 两者的值相等,但不全等,因为类型不同,前者为 string 类型,后者为 object 类型console.log( str2.valueOf() === str2 ); // false
