Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular.isDefined() vs obj.hasOwnProperty()

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);
like image 887
user3040068 Avatar asked Oct 06 '14 15:10

user3040068


People also ask

What is the difference between in and hasOwnProperty in JavaScript?

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.

How to check if an object has own property in JavaScript?

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:

What is the use of isdefined function in AngularJS?

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.

What is the purpose of hasOwnProperty method?

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.


1 Answers

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

like image 141
Jscti Avatar answered Sep 21 '22 15:09

Jscti