Home > Web Front-end > Front-end Q&A > what is javascript uber

what is javascript uber

藏色散人
Release: 2021-11-01 15:31:48
Original
1697 people have browsed it

javascript uber is a method used in early javascript to let a method call the parent class. The uber method is similar to Java's super.

what is javascript uber

The operating environment of this article: Windows 7 system, JavaScript version 1.8.5, DELL G3 computer.

javascriptWhat is uber?

In early JavaScript, the uber method is similar to Java's super, which allows a method to call the method of the parent class. Douglas Crockford used the German "über", which means something similar to super, to avoid conflict with reserved words.

However, Crockford also said that the idea of ​​super is very important in the classic design pattern, but it seems unnecessary in the prototype and function design patterns of JavaScript. Classical Inheritance in JavaScript Classic object-oriented languages ​​generally have special syntax for accessing the parent class (super class), so that the methods of the subclass can use the methods of the parent class, and the methods of the subclass and the parent class have the same name. In modern JavaScript, there is no such special syntax. Uber can implement this function, but it is more cumbersome. Look at the following example:

// inheritance helper
function extend(Child, Parent) {
  var F = function () {};
  F.prototype = Parent.prototype;
  Child.prototype = new F();
  Child.prototype.constructor = Child;
  Child.uber = Parent.prototype;
}
// define -> augment
function Shape() {}
Shape.prototype.name = 'Shape';
Shape.prototype.toString = function () {
  return this.constructor.uber
  ? this.constructor.uber.toString() + ', ' + this.name
  : this.name;
};
// define -> inherit -> augment
function TwoDShape() {}
extend(TwoDShape, Shape);
TwoDShape.prototype.name = '2D shape';
// define
function Triangle(side, height) {
  this.side = side;
  this.height = height;
}
// inherit
extend(Triangle, TwoDShape);
// augment
Triangle.prototype.name = 'Triangle';
Triangle.prototype.getArea = function () {
  return this.side * this.height / 2;
};
Copy after login

Input in the Console:

var my = new Triangle(5, 10);
my.toString();
Copy after login

Output: "Shape, 2D shape, Triangle"

The derived level is: Shape -> TwoDShape -> Triangle

The extend function encapsulates the inherited code.

The role of the temporary constructor F(): when the properties of the subclass change, the properties of the parent class are not changed.

uber attribute: points to the parent class prototype.

In the toString() method, check whether the prototype of the parent class of the constructor exists. If it exists, call its toString() method, thereby realizing calling the parent class method in the subclass.

Recommended study: "javascript basic tutorial"

The above is the detailed content of what is javascript uber. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
What are JavaScript hook functions?
From 1970-01-01 08:00:00
0
0
0
What is JavaScript garbage collection?
From 1970-01-01 08:00:00
0
0
0
c++ calls javascript
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template