JavaScript MCQ Javascript Classes MCQ Question #5013
Single Choice Easy

QWhat would the following JavaScript code produce?

function emp(name,salary)
{  
     this.name=name;  
     this.salary=salary;  
 
     this.changeSalary=changeSalary;  
     function changeSalary(otherSalary)
     {  
         this.salary=otherSalary;  
     }  
}  
e=new emp("Rahul",30000);  
e.changeSalary(45000);  
document.write("e.name+" "+e.salary);

ID: #5013 Javascript Classes MCQ 181 views
Question Info
#5013Q ID
EasyDifficulty
Javascript Classes MCQTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A Rahul 30000
  • B Rahul
  • C Rahul 45000
  • D v
Correct Answer

Explanation


The emp function is defined as a constructor function that takes two arguments: name and salary.
The name and salary properties of the object are set using the this keyword.
The changeSalary method is defined and assigned to the changeSalary property of the object using the this keyword. The changeSalary method takes a single argument: otherSalary.
An object is created using the emp constructor function and passing it the arguments "Rahul" and 30000. 
This creates an object with the name property set to "Rahul" and the salary property set to 30000.
The changeSalary method is called on the object, passing it the argument 45000. This sets the salary property of the object to 45000.
The name and salary properties of the object are written to the document using the document.write() method. The output is e.name+ 45000.


The changeSalary method uses the this keyword to refer to the current object and sets the salary property of the object to the value of the otherSalary argument. 
This allows you to change the salary of the object by calling the changeSalary method and passing it a new salary value

Share This Question

Challenge a friend or share with your study group.