var obj = {a: undefined,b: null,c: false};
// a, b, c all foundfor ( var prop in obj ) {document.writeln( "Object1: " + prop );}
function Class(){this.a = undefined;this.b = null;this.c = false;}
Class.prototype = {a: undefined,b: true,c: true,d: true,e: true};
var obj2 = new Class();
// a, b, c, d, e foundfor ( var prop in obj2 ) {document.writeln( "Object2: " + prop );}
function hasOwnProperty(obj, prop) {var proto = obj.__proto__ || obj.constructor.prototype;return (prop in obj) &&(!(prop in proto) || proto[prop] !== obj[prop]);}
if ( Object.prototype.hasOwnProperty ) {var hasOwnProperty = function(obj, prop) {return obj.hasOwnProperty(prop);}}
// a, b, c found in modern browsers// b, c found in Safari 2.0.1 and olderfor ( var prop in obj2 ) {if ( hasOwnProperty(obj2, prop) ) {document.writeln( "Object2 w/ hasOwn: " + prop );}}
/**Gets an argument from array or object.The possible outcome:- If the key exists the value is returned.- If no key exists the default value is returned.- If no default value is specified an empty string is returned.@param obj The object or array to be searched.@param key The name of the property or key.@param defVal Optional default version of the command-line parameter [default ""]@return The default value in case of an error else the found parameter.*/function getSafeReflectArg( obj, key, defVal) {"use strict";var retVal = (typeof defVal === 'undefined' ? "" : defVal);if ( Reflect.has( obj, key) ) {return Reflect.get( obj, key);}return retVal;} // getSafeReflectArg
const has = Object.prototype.hasOwnProperty; // Cache the lookup once, in module scope.console.log(has.call(object, key));/* Or */import has from 'has'; // https://www.npmjs.com/package/hasconsole.log(has(object, key));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js" integrity="sha512-90vH1Z83AJY9DmlWa8WkjkV79yfS2n2Oxhsi2dZbIv0nC4E6m5AbH8Nh156kkM7JePmqD6tcZsfad1ueoaovww==" crossorigin="anonymous"> </script>
This shippet only presents functions used in performance tests - it not perform tests itself!
如果你想在不使用hasOwnProperty()检查的情况下迭代对象的属性,使用for(let key of Object.keys(stud)){}方法:
for(let key of Object.keys(stud)){console.log(key); // will only log object's Own properties}
完整示例和与for-in with hasOwnProperty()的比较
function Student() {this.name = "nitin";}
Student.prototype = {grade: 'A'}
let stud = new Student();
// for-in approachfor(let key in stud){if(stud.hasOwnProperty(key)){console.log(key); // only outputs "name"}}
//Object.keys() approachfor(let key of Object.keys(stud)){console.log(key);}
let obj1 = {prop:undefined};console.log(1,"prop" in obj1);console.log(1,obj1?.prop);
let obj2 = undefined;//console.log(2,"prop" in obj2); would throw because obj2 undefinedconsole.log(2,"prop" in (obj2 ?? {}))console.log(2,obj2?.prop);
let obj3 = {prop:false};console.log(3,"prop" in obj3);console.log(3,!!obj3?.prop);
let obj4 = {prop:null};let look = "prop"console.log(4,"prop" in obj4);console.log(4,obj4?.[look]);
let obj5 = {prop:true};console.log(5,"prop" in obj5);console.log(5,obj5?.prop === true);
let obj6 = {otherProp:true};look = "otherProp"console.log(6,"prop" in obj6);console.log(6,obj6.look); //should have used bracket notation
let obj7 = {prop:""};console.log(7,"prop" in obj7);console.log(7,obj7?.prop || "empty");