簡體   English   中英

我試圖從javascript對象中檢索“員工信息”,但是在檢查javascript控制台時出錯

[英]I have tried to retrieve `employee info` from javascript object but error while checking javascript console

 // the Employee object function Employee( name, email, role ) { this.name = name; this.email = email; this.role = role; } Employee.prototype.helloEmployee = function() { console.log( "Oye" + role); } // the emp object inherits from Employee function emp( name, email ) { Employee.call(this, name, email, "admin"); } emp.prototype = Object.create( Employee.prototype ); var kumar = new emp( "Kumar", "info@helloitskumar.com" ); kumar.helloEmployee(); 

我試圖從javascript對象檢索employee info ,但在檢查javascript控制台時出錯。 您能幫我解決代碼問題嗎?

您忘記在console.log中添加this參考。 檢查這個工作的jsfiddle

 // the Employee object function Employee( name, email, role ) { this.name = name; this.email = email; this.role = role; } Employee.prototype.helloEmployee = function() { console.log( "Oye" + this.role); } // the emp object inherits from Employee function emp( name, email ) { Employee.call(this, name, email, "admin"); } emp.prototype = Object.create( Employee.prototype ); var kumar = new emp( "Kumar", "info@helloitskumar.com" ); kumar.helloEmployee(); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM