0

Here, I created an object say d.

var d={
  a:"firstName",
  b:"lastName"
};

Now I want to create another object say A, which inherits properties of d.

var A=Object.create(d);

console.log(A.a);//returns "firstName"
console.log(A.b);//returns "lastName"

But when I uses console.log(A);// returns empty object, as it doesn't show inherited properties.

But it creates little problem while using with angular.forEach.

I want to use angular.forEach which parse all properties including inherited properties. How can I loop through object including parent properties?

I have to use Object.create as parent object is dynamic, i.e. It may include more objects in future, and these properties will automatically comes in child object. Here I cant use angular.copy as it does deep copy and breaks relation between parent object.

In previous version of google chrome, see inherited properties also. But after updating to version 43.0.2357.52, its not showing inherited properties in console

Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65

4 Answers4

1

I don't know if Angular has a specific function for it (I didn't see one), but you can use JavaScript's for-in loop to see those:

var key;
for (key in d) {
    // key will be "firstName" on one pass and "lastName" on another
    // there IS NO guarantee of order
}

for-in visits both own properties and inherited ones.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
1

You should to use angular.copy() to do this:

var A = angular.copy(d);
Joao Polo
  • 2,153
  • 1
  • 17
  • 26
0

The angular.forEach does not iterate over inherited properties and it does not have any function that does it.

... It is worth noting that .forEach does not iterate over inherited properties because it filters using the hasOwnProperty method... angular 1.4

You need implement you own function.

Victor Aguilar
  • 455
  • 4
  • 18
-1
 var values = {name: 'misko', gender: 'male'};
var log = [];
angular.forEach(values, function(value, key) {
  this.push(key + ': ' + value);
}, log);
expect(log).toEqual(['name: misko', 'gender: male']);

I found this example on documentation page

zooblin
  • 2,172
  • 2
  • 27
  • 33