Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of static method of javascript Class

Shulou Source: shulou.com Published: 2022-06-01 01:19:09 09月21日 Update

In this article Xiaobian for you to introduce in detail the "javascript class static method instance analysis", the content is detailed, the steps are clear, the details are handled properly, I hope this "javascript class static method instance analysis" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

The static method of a class in JavaScript is a method modified with the static keyword, also called a class method; before instantiating an object, a static method can be called through the class name. method name, and static methods cannot be called on the object, only in the class.

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

Javascript class static method

A static method is a method modified by the static keyword, also known as a class method, which belongs to a class, but does not belong to an object, and can be instantiated by the class name. The static method is called by the method name.

Static methods cannot be called on an instance of a class, but should be called through the class itself. Cannot be called on an object, it can only be called in a class.

Examples are as follows:

Class Tripple {static tripple (n = 1) {return n * 3;}} / / correct usage console.log (Tripple.tripple ()); / / 3Accord / the following is the wrong use let tp = new Tripple (); console.log (tp.tripple ()); / / 'tp.tripple is not a function'.

An error will be reported when the instance object calls a static method:

Read this, the "javascript class static method instance analysis" article has been introduced, want to master the knowledge of this article also need to practice and use to understand, if you want to know more about the article, welcome to follow the industry information channel.

Tags: Method static instance object instance analysis analysis article key keyword content can pass class name proper function train of thought tutorial new knowledge more step environment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MySQL Linux Redmi Apple