Call and apply usage examples in javascript: javascriptapply
Call and apply usage, and use call to implement js class inheritance
/** Rectangle */function Rectangle (len, width) {this. len = len; this. width = width;}/** multiplied by */function multiply (a, B) {return a * B;} // var rectangle = new Rectangle (15, 30) of the rectangular instance ); // calculate the rectangular area var proportion = multiply. call (rectangle, rectangle. len, rectangle. width); // equivalent to call // var proportion = multiply. apply (rectangle, [rectangle. len, rectangle. width]); document. write ("the area of the rectangle is:" + proportion); document. write ("<br/>"); document. write ************ * *******************/<br/> "); // implement inherited function Persion (name) {this. name = name; this. sayHello = function () {return "hello," + this. name ;}} function Student (name, sex, school) {Persion. call (this, name); this. sex = sex; this. school = school; this. mySex = function () {return this. sex;} this. mySchool = function () {return this. school;} var stu = new Student ('fengjx', 'male', 'guangxi Emy of mechanical and electrical technology ') document. write ("stu sayHello:" + stu. sayHello (); document. write ("<br/>"); document. write ("stu sex is:" + stu. mySex (); document. write ("<br/>"); document. write ("stu school is:" + stu. mySchool (); document. write ("<br/> ");
Demo diagram:
The above is all the content of this article. I hope you will like it.