2017-06-12 56 views
0

怎么看控制台的JavaScript

function Person() { 
 
    this.firstName; 
 
    this.lastName; 
 
    this.age; 
 
    this.eyeColor; 
 
    this.nationality; 
 
} 
 
var luffy = new Person(); 
 
luffy.firstName="Monkey"; 
 
console.log(luffy);

在此,如果我在打开控制台,它会显示一个的firstName原型的身体,但我在哪里可以看到该对象的身体,我的意思是哪里我可以看到有什么在这个对象原型...因为我们可以看到构造函数,如果我们打开对象proto,同样的方式,我想看到这个对象的父母的身体...请帮助我(我还没有给任何东西作为原型中的一个参数,即人)

+2

如果展开对象,它应该表现出'__proto__'属性。这是原型。还要注意,所有'this.firstName; this.lastName; ......“陈述绝对没有任何意义。 – Ryan

+1

构造函数中的所有这些语句评估为“未定义”并且没有副作用。他们不会以任何方式修改对象或其原​​型。 – Paulpro

+1

'luffy.constructor'? –

回答

2

使用> luffy.constructor将返回构造函数的定义。

使用> luffy.__proto__来了解对象的原型。

function Person() { 
 
    this.firstName; 
 
    this.lastName; 
 
    this.age; 
 
    this.eyeColor; 
 
    this.nationality; 
 
} 
 
var luffy = new Person(); 
 
luffy.firstName="Monkey"; 
 

 
console.log(luffy.constructor); // returns the constructor i.e., is the definition of the function contructor

1

您可以使用Object.constructor返回创建实例obj的构造函数等。

你的情况:

> luffy.contructor 

< function Person() { 
    this.firstName; 
    this.lastName; 
    this.age; 
    this.eyeColor; 
    this.nationality; 
    }