JavaScript simple Implementation Object-oriented programming inheritance instance code _javascript skill

Source: Internet
Author: User

This article describes the JavaScript simple implementation object-oriented programming inheritance instance code. Share to everyone for your reference, specific as follows:

An object-oriented language must have four basic characteristics:

1. Encapsulation capability (that is, allowing a variable or function of a basic data type to be placed in a class to form a member or method of a class)
2. Aggregation capability (that is, allow classes to be included in the class so that it can cope with complex designs)
3. Support Inheritance (a parent class can derive a subclass, a subclass with a parent's property or method)
4. Support polymorphism (allow the same method name, according to the method signature [i.e. function parameters] different, have their own independent processing method)

These four basic properties, JavaScript can support, so JavaScript is really a weak type of object-oriented language, here gives a simple class inheritance code

<script type= "Text/javascript" >
//Parent class ClassA
function ClassA (scolor) {
  this.color = Scolor;
  This.saycolor = function () {
    document.write ("Color:" + this.color + "<br/>");
  }
Subclass ClassB, inheriting from ClassA function
ClassB (scolor,sname) {  
  classa.call (this,scolor);//using the Call function, All the methods of ClassA are assigned to CLASSB, that is, to realize the inheritance
  this.name = sname;
  This.sayname = function () {
    document.write ("Name:" + this.name + "<br/>");
  }
var Oclassa = new ClassA ("Red");
Oclassa.saycolor ();
var oclassb = new ClassB ("Blue", "Jimmy.yang");
Oclassb.saycolor ()///Here the Saycolor method is inherited from the ClassA
oclassb.sayname ();//This is the new method in CLASSB/
*
Demo Example of the Call function
function Saycolor (Sprefix, ssuffix) {
alert (sprefix + This.color + ssuffix);
var obj = new Object ();
Saycolor.call (obj, "The color is", ", a very nice color indeed.");
* *
</script>

I hope this article will help you with JavaScript programming.

Related Article

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.