There are several answers here how to check if a property exists in an object.
I was always using
if(myObj.hasOwnProperty(‘propName’))
but I wonder if there is any difference from
if(‘propName’ in myObj){
An example
1 | var test = function() {} |
another example
1 | var o = {"name":"sun nan","university":"Deakin","course":"Bachelor of Information Technology (Programming)-Deakin","email":"417757848@qq.com","first_name":"Nan","last_name":"Sun","date_of_birth":"1993-09-13"} |
undefined error will be raise
next example
1 | //在JavaScript的世界中,所有的函数都能作为构造函数,构造出一个对象 |