Mega Code Archive

 
Categories / JavaScript Tutorial / Function
 

Function apply()

Syntax function.apply() The apply() method is used to apply a method of one object to another object. Using the apply() method keeps developers from rewriting methods for different objects. <html> <head> <title>Example</title> </head> <body> <script type="text/javascript"> function ClassA(sColor) {     this.color = sColor;     this.sayColor = function () {         alert(this.color);     }; } function ClassB(sColor, sName) {     ClassA.apply(this, arguments);     this.name = sName;     this.sayName = function () {         alert(this.name);     }; } var objA = new ClassA("red"); var objB = new ClassB("blue", "Nicholas"); objA.sayColor(); objB.sayColor(); objB.sayName(); </script> </body> </html>