JavaScript implements a method similar to OOP inheritance based on prototype _javascript tips

Source: Internet
Author: User
Tags modifier

The examples in this article describe JavaScript's implementation of a method similar to OOP inheritance based on prototype. Share to everyone for your reference, specific as follows:

The public properties (using this. modifier) can be overridden, and private properties (using the Var modifier) cannot be overridden

Subclasses cannot access the private properties of the parent class, and the parent class's methods normally access the private variables of the parent class.

function Vegetable () {
  this.taste= ' delicious ';
  var a = ' i\ ' m vegetable\ ' a!
  ' THIS.FUN1 = function () {
    alert (' Vegetable fun1 doing ... ');
  }
  This.fun3 = function () {
    alert (a);
  }
}
function celery () {
  var a = ' i\ ' m celery\ ' a ';
  This.color = ' green ';
  This.taste = ' bad ';
  THIS.FUN1A = function () {
    alert (' Celeryfun1 doing ... ');
  }
  this.fun2 = function () {
    alert (' Celery fun2 doing ... ');
  }
  This.fun4 = function () {
    alert (a);
  }
}
Celery.prototype = new Vegetable ();
var stick = new celery ();
var polymorphed = stick.taste;
alert (polymorphed);
alert (stick.color);
Stick.fun1 ();
Stick.fun2 ();
Stick.fun3 ();
Stick.fun4 ();

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.