I have an object that may or may not have a status. When using the angular.js framework which would be more appropriate. What are the advantages and disadvantages of both.
var checkStatus = function(item){
if(angular.isDefined(item.status){
//do something
}
//VS.
if(item.hasOwnProperty('status')){
//do something
}
}
checkStatus(item);
Returns a Boolean indicating whether or not the object has the specified property as its own property. Unlike the in operator, this method does not check for a property in the object's prototype chain. hasOwnProperty returns true even if the value of the property is null or undefined.
The JavaScript Object hasOwnProperty () method checks if the object has the given property as its own property. The syntax of the hasOwnProperty () method is: Here, obj is an object. The hasOwnProperty () method takes in:
The angular.isDefined() function in AngularJS is used to determine the value inside isDefined function is defined or not. It returns true if the reference is a defined otherwise returns false. Syntax: angular.isDefined( value ) Return Value: It returns true if the passed value is defined otherwise returns false.
Description. Every object descended from Object inherits the hasOwnProperty method. This method can be used to determine whether an object has the specified property as a direct property of that object; unlike the in operator, this method does not check down the object's prototype chain.
angular.isDefined
only test if the value is undefined
:
function isDefined(value){return typeof value !== 'undefined';}
Object.hasOwnProperty
test if the value is a direct one and not an inherited one.
For example :
var test = {};
angular.isDefined(test.toString); // true
test.hasOwnProperty('toString'); // false
info : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With