简体   繁体   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(); 

I have tried to retrieve employee info from javascript object but error while checking javascript console. 我试图从javascript对象检索employee info ,但在检查javascript控制台时出错。 Can you help what's wrong with my code? 您能帮我解决代码问题吗?

Your forgot to add this reference in console.log . 您忘记在console.log中添加this参考。 check this working jsfiddle 检查这个工作的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