JS how to tell if an object {} is an empty object, without any attributes
The previous time with JS wrote a similar "Angularjs" for data binding things, the function is relatively simple,
Usually it should come in an array of ArrayList JSON objects,
But sometimes a JSON object is returned through an Ajax method call, not an array!
In order to be compatible with this scenario, the following code is used:
if (typeof model.rows = = = = "Object" &&! Model.rows instanceof Array) {model.rows = [model.rows];}
This code found a bug in the process of later use, that is, when
Model.rows = {};
, it is still treated as a valid object for data binding, and it is conceivable that all data is empty.
Solution:
So how do we solve this problem?
The first habit is to search first, but the results are DOM-related, passable!
This reminds me of the way I wrote the properties of the JS object, which can be used here!
The code is as follows:
if (typeof model.rows = = = = "Object" &&! Model.rows instanceof Array) {var hasprop = False;for (var prop in model.rows) {hasprop = True;break;} if (hasprop) {model.rows = [model.rows];} Else{throw "Model.rows is empty object"; return false;}}
The code is simple and does not write comments.
JS how to tell if an object {} is an empty object, without any attributes