hasOwnProperty: 判断一个对象是否包含某个属性(私有属性)(不包括原型链上的属性)
const obj = { a: 2 }obj.hasOwnProperty('a') // trueobj.hasOwnProperty('b') //false
function Fn(x,y){let sum = 10;this.total = x + y;this.say =function(){console.log(this.total)}}let f1 = new Fn()let oo = {name: "oo",fn: function () {console.log(this.name);}}console.log(f1.hasOwnProperty('say')) //true say构造时已经是f1的私有属性console.log(f1.hasOwnProperty('hasOwnPropery')) //false hasOwnPropery是原型链上的共有属性console.log(f1.hasOwnProperty('sum')); //falseconsole.log(oo.hasOwnProperty('name')); //trueconsole.log(oo.hasOwnProperty('fn')); //trueconsole.log(Fn.prototype.__proto__.hasOwnProperty('hasOwnProperty')); //true 首先Fn是函数它的 __proto__ 是 ƒ () 它的 prototype 是constructor: ƒ Fn(x, y) 和 __proto__: Object => 这里才有hasOwnProperty方法let obj = new Object(oo)console.log(obj.hasOwnProperty('name')); //trueconsole.log(obj.hasOwnProperty('fn')); //trueconsole.log('name' in obj) //true
isPrototypeOf: 方法用于测试一个对象(是一个对象)是否存在于另一个对象的原型链上。
function fn() {}_fn1 = new fn() // fn {}_fn2 = fn() // undefinedconsole.log(fn.isPrototypeOf(_fn1)) // falseconsole.log(fn.prototype.isPrototypeOf(_fn1)) // trueconsole.log(fn.prototype.isPrototypeOf(_fn2)) // falseconsole.log(Object.prototype.isPrototypeOf(_fn1)) // trueconsole.log(Object.prototype.isPrototypeOf(_fn2)) // false
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
in 如果指定的属性在指定的对象或其原型链中(无论是公有还是私有),则in 运算符返回true。
const car = { make: 'Honda', model: 'Accord', year: 1998 };console.log('make' in car);// expected output: truedelete car.make;if ('make' in car === false) {car.make = 'Suzuki';}console.log(car.make);// expected output: "Suzuki"
// 数组var trees = new Array("redwood", "bay", "cedar", "oak", "maple");0 in trees // 返回true3 in trees // 返回true6 in trees // 返回false 超出长度"bay" in trees // 返回false (必须使用索引号,而不是数组元素的值)"length" in trees // 返回true (length是一个数组属性)Symbol.iterator in trees // 返回true (数组可迭代,只在ES2015+上有效)// 内置对象"PI" in Math // 返回true// 自定义对象var mycar = {make: "Honda", model: "Accord", year: 1998};"make" in mycar // 返回true"model" in mycar // 返回true
in右操作数必须是一个对象值。例如,你可以指定使用String构造函数创建的字符串,但不能指定字符串文字。
var color1 = new String("green"); //new 出来的都是对象"length" in color1 // 返回truevar color2 = "coral";"length" in color2 // 报错(color2不是对象)
对被删除或值为 undefined 的属性使用in
如果你使用 [delete](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/delete) 运算符删除了一个属性,则 in 运算符对所删除属性返回 false。
var mycar = {make: "Honda", model: "Accord", year: 1998};delete mycar.make;"make" in mycar; // 返回falsevar trees = new Array("redwood", "bay", "cedar", "oak", "maple");delete trees[3];3 in trees; // 返回false
如果你只是将一个属性的值赋值为undefined,而没有删除它,则 in 运算仍然会返回true。
var mycar = {make: "Honda", model: "Accord", year: 1998};mycar.make = undefined;"make" in mycar; // 返回true
var trees = new Array("redwood", "bay", "cedar", "oak", "maple");trees[3] = undefined;3 in trees; // 返回true
继承属性
如果一个属性是从原型链上继承来的,in 运算符也会返回 true。
"toString" in {}; // 返回true
