2017-06-12 33 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; 
    }