当打印整个对象时,为什么console.log不显示PROTOTYPES在javascript中添加的属性值? [英] why console.log doesn't show values of properties added by PROTOTYPES in javascript when whole object is printed?

查看:59
本文介绍了当打印整个对象时,为什么console.log不显示PROTOTYPES在javascript中添加的属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Prototype用于添加属性或方法,JavaScript对象从其原型继承其属性和方法.

I know Prototype is used to add properties or methods and JavaScript objects inherit their properties and methods from their prototype.

var  Person=function (name) {

this.Fname=name;
this.health=100;

};
//create object of Person
var Mateen=new Person("Mateen");
var Fariza=new Person("Fariza");


console.log(Mateen);
console.log(Fariza);

//output { Fname: 'Mateen', health: 100 }
         { Fname: 'Fariza', health: 100 }

//Adding method by prototype
Person.prototype.attack=function  attack(target) {
target.health-=2;
}

Fariza.attack(Mateen);

console.log(Mateen);
console.log(Fariza);

//output: { Fname: 'Mateen', health: 98 }
          { Fname: 'Fariza', health: 100 }

//adding proterty by prototype
Person.prototype.level=1;

console.log(Mateen);
//output: { Fname: 'Mateen', health: 98 }

在最后一个输出中,为什么在打印整个对象时它不显示水平.它确实显示了何时使用console.log(Mateen.level).

In last output why it doesn't show level when whole object is printed. It do shows when console.log(Mateen.level) is used.

推荐答案

我猜这可能取决于浏览器,但是在 Chrome 中,您可以通过展开 __ proto __ 来查看它-对象直接在控制台中.

I guess this can depend on the browser but in Chrome you can view it by expanding the __proto__-object directly in the console.

Firefox 中,您可以单击输出,并在右侧/底部获得与chrome类似的输出.

In Firefox you can click on the output, and get a similar output as chrome to the right/bottom.

Safari 与Chrome类似

不幸的是,我无法验证IE/Edge

Unfortunately I can't verify IE/Edge

这篇关于当打印整个对象时,为什么console.log不显示PROTOTYPES在javascript中添加的属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆