Call and apply usage examples in javascript: javascriptapply

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.