Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is javascript uber?

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is javascript uber". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Javascript uber is a method used in early javascript to have a method call a parent class, and the uber method is similar to Java's super.

This article operating environment: windows7 system, javascript1.8.5 version, DELL G3 computer.

What is javascript uber?

In early JavaScript, the uber method was similar to Java's super in that it allowed a method to call the method of the parent class. Douglas Crockford uses the German word "ü ber", which means similar to super and avoids conflicts with reserved words.

However, Crockford also said that the idea of super is important in classical design patterns, but it is not necessary in JavaScript prototypes and functional design patterns. Classical Inheritance in JavaScript classic object-oriented languages generally have a special syntax for accessing the parent class (superclass), so the methods of the class 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, without this special syntax, uber can achieve this function, but it is more tedious. Take a look at the following example:

/ / inheritance helperfunction extend (Child, Parent) {var F = function () {}; F.prototype = Parent.prototype; Child.prototype = new F (); Child.prototype.constructor = Child; Child.uber = Parent.prototype;} / / define-> augmentfunction Shape () {} Shape.prototype.name = 'Shape';Shape.prototype.toString = function () {return this.constructor.uber? This.constructor.uber.toString () +','+ this.name: this.name;}; / / define-> inherit-> augmentfunction TwoDShape () {} extend (TwoDShape, Shape); TwoDShape.prototype.name ='2D shape';// definefunction Triangle (side, height) {this.side = side; this.height = height;} / / inheritextend (Triangle, TwoDShape); / / augmentTriangle.prototype.name = 'Triangle';Triangle.prototype.getArea = function () {return this.side * this.height / 2;}

In Console, enter:

Var my = new Triangle (5,10); my.toString ()

Output: "Shape, 2D shape, Triangle"

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

The function extend 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.

The uber attribute: points to the parent class prototype.

In the toString () method, check whether the prototype of the parent class of the constructor exists, and if so, call its toString () method, thus implementing the invocation of the parent method in the subclass.

This is the end of "what is javascript uber". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report