Javascript object-oriented-inheritance

Source: Internet
Author: User

JavaScript is not an object-oriented language, but we construct it to support object-oriented, so that we can implement object-oriented features such as inheritance, rewriting, and so on. The specific code is as follows:

//create a class personfunctionPerson (age,name) { This. age=Age ;  This. name=name;  This. sayhello=function() {Console.log ("My lord, Hello, I call" + This. name+ ", this year" + This. age+ "Years old"); }}//Create class student, add attribute numberfunctionStudent (Age,name,stuno) { This. stuno=Stuno; Person.call ( This, age,name);//inherit the person class, taking care to differentiate between call and apply        //rewrite SayHello     This. sayhello=function() {Console.log ("Good student everyone, I call" + This. name+ ", this year" + This. age+ "years old, school number is:" + This. Stuno); }}varp=NewPerson (20, "John Doe");p. SayHello ();varstu=NewStudent (22, ' Harry ', ' 123456 '); Stu.sayhello ();

This small piece of code can elicit the difference between call and apply, the scope of this in JavaScript, and so on.

Javascript object-oriented-inheritance

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.