访问angular.forEach中的父属性

时间:2023-02-05 02:32:26

Here, I created an object say d.

在这里,我创建了一个对象说d。

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

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

现在我想创建另一个对象A,它继承了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.

但是当我使用console.log(A); //返回空对象时,因为它没有显示继承的属性。

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

但是使用angular.forEach时它几乎没有问题。

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

我想使用angular.forEach来解析包括继承属性在内的所有属性。如何循环包括父属性的对象?

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.

我必须使用Object.create,因为父对象是动态的,即它将来可能包含更多对象,并且这些属性将自动出现在子对象中。在这里,我不能使用angular.copy,因为它深度复制并打破父对象之间的关系。

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

在以前版本的Google Chrome中,请参阅继承的属性。但在更新到版本43.0.2357.52后,它没有在控制台中显示继承的属性

4 个解决方案

#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:

我不知道Angular是否具有特定的功能(我没有看到),但您可以使用JavaScript的for-in循环来查看:

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.

for-in访问自己的属性和继承的属性。

#2


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

你应该使用angular.copy()来做到这一点:

var A = angular.copy(d);

#3


 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

我在文档页面上找到了这个例子

#4


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

angular.forEach不会迭代继承的属性,也没有任何执行它的函数。

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

...值得注意的是.forEach不会迭代继承的属性,因为它使用hasOwnProperty方法进行过滤... angular 1.4

You need implement you own function.

你需要实现自己的功能。

#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:

我不知道Angular是否具有特定的功能(我没有看到),但您可以使用JavaScript的for-in循环来查看:

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.

for-in访问自己的属性和继承的属性。

#2


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

你应该使用angular.copy()来做到这一点:

var A = angular.copy(d);

#3


 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

我在文档页面上找到了这个例子

#4


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

angular.forEach不会迭代继承的属性,也没有任何执行它的函数。

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

...值得注意的是.forEach不会迭代继承的属性,因为它使用hasOwnProperty方法进行过滤... angular 1.4

You need implement you own function.

你需要实现自己的功能。