Methods in JavaScript
JavaScript is an object-based language, and classes are one of its core concepts. Classes contain properties and methods. A class method is a function defined in the class. They are called the behavior of the object. They can operate on the properties of the object to process data.
In JavaScript, class methods are defined in the prototype of the class, so each instance object can access these methods without having to define them repeatedly. This is also the object-oriented programming (OOP) in JavaScript. ) is one of the important features.
How to define a class
It is very simple to define a class method in JavaScript. You only need to define a function on the prototype object of the class, for example:
class Car { constructor(brand, price) { this.brand = brand; this.price = price; } getInfo() { console.log(`The brand of this car is ${this.brand}, and the price is ${this.price}`); } } let myCar = new Car("BMW", 50000); myCar.getInfo(); // 输出:The brand of this car is BMW, and the price is 50000
In In this example, we define a method named getInfo
, which uses the console.log
function to output the brand and price of the car. When the getInfo()
method is called on the instance object of the class, the corresponding information will be printed out.
Accessing the attributes of the class
In the method of the class, you can directly access and modify the attributes of the class, for example:
class Car { constructor(brand, price) { this.brand = brand; this.price = price; } getInfo() { console.log(`The brand of this car is ${this.brand}, and the price is ${this.price}`); } updatePrice(newPrice) { this.price = newPrice; } } let myCar = new Car("BMW", 50000); myCar.updatePrice(55000); myCar.getInfo(); // 输出:The brand of this car is BMW, and the price is 55000
In this example, we define a name Use the updatePrice
method to update the car price. This method accepts a new price parameter and assigns it to the price
property of the object. Then, by calling the getInfo
method, we can view the car's brand and updated price.
Keywordthis
In the above example, we used the keyword this
to refer to the current object (that is, the object on which the method is called) . In JavaScript, this
is a keyword that points to the current object. Its specific pointer is determined through the call stack at runtime.
For example, when myCar.getInfo()
is called, this
points to the myCar
object. When the updatePrice
method is called, this
also points to the myCar
object. By using this
, we can easily access the properties and methods of the current object.
Static methods of classes
In addition to instance methods, JavaScript also supports static methods of classes. Static methods are methods that can be accessed directly without instantiating the object. They are generally used to handle class-related tasks.
In JavaScript, static methods can be defined by adding the static
modifier to the definition of the class, for example:
class Car { constructor(brand, price) { this.brand = brand; this.price = price; } getInfo() { console.log(`The brand of this car is ${this.brand}, and the price is ${this.price}`); } static getBrand() { console.log("The brand of this car is BMW"); } } Car.getBrand(); // 输出:The brand of this car is BMW
In this example, we define a static Method getBrand
, which directly outputs the car's brand information without instantiating the car object. Just call the static method directly through the class name.
Summary
The method of a class is one of the core concepts in OOP programming. It can operate on the attributes of the class and implement data processing. JavaScript defines class methods through the class prototype, and each instance object can access these methods without repeated definitions. At the same time, JavaScript also supports static methods of classes, which can be accessed directly by class name without instantiating the object.
The above is the detailed content of Class method javascript. For more information, please follow other related articles on the PHP Chinese website!