Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript how to remove prototype methods from a data object or make objects work with Qunit equals

I've started working with Qunit to do unit testing on my code, but am striking problems comparing objects.

The code I'm testing dynamically creates an object:

var fields = ['id','name'];
var result = {};
for (var field in fields)
{
    var name = fields[field];
    result[name] = name;
}

var expected = { id : 'id', name : 'name' };

test(expected, result, "same ?");

This test fails, and I can see that while expected just contains the fields id and name, result also contains a whole lot of functions, eg

"function (iterator, context) {...

I think these functions come from me treating the object like an array in order to assign to it.

Is there a way to strip these functions out (or prevent them from getting added in the first place)?

I was thinking of doing

result = $.parseJSON($.toJSON(result));

is there a better way?

like image 230
Chris J Avatar asked Dec 17 '10 03:12

Chris J


People also ask

How do you set the prototype of one object to another in JavaScript?

setPrototypeOf() The Object. setPrototypeOf() method sets the prototype (i.e., the internal [[Prototype]] property) of a specified object to another object or null .

How do you create an object without a prototype?

The solution is to create an object without a prototype: var dict = Object. create(null);

What is the difference between __ proto __ and prototype?

The prototype property is set to function when it is declared. All the functions have a prototype property. proto property that is set to an object when it is created using a new keyword. All objects behavior newly created have proto properties.

Is __ proto __ deprecated?

prototype. __proto__ Deprecated: This feature is no longer recommended.


2 Answers

You can check the type of each object in the collection to see if it's a function:

if(typeof fields[field] == 'function') continue;

Also, don't use for..in on arrays:

for(var i = 0; i < fields.length; i++) {
   result[result[i]] = result[i];
}

What you have there isn't strictly an object, it's an Array object, which is nevertheless an object, but not in the sense that it has key-value pairs.

If you use a vanilla for loop, you will not iterate through the prototype chain as you do with for..in, so maybe doing that might solve your problem.

like image 188
Jacob Relkin Avatar answered Sep 19 '22 17:09

Jacob Relkin


The problem is your use of for...in, which is iterating over everything fields is inheriting from object. If you must keep for...in, you can modify it like this:

for (var field in fields){
  if( fields.hasOwnProperty( field ) ){
    var name = fields[field];
    result[name] = name;
  }
}

hasOwnProperty returns true if the specified property of the object (and the array is an object) is not inherited.

like image 20
Ken Redler Avatar answered Sep 21 '22 17:09

Ken Redler