Home > Article > Web Front-end > How to return value from constructor in javascript
JavaScript is a dynamic programming language, one of its important features is the support of object-oriented programming (Object-Oriented Programming, OOP). Like other programming languages, the constructor in JavaScript plays an important role in creating objects. Constructors not only define the properties and methods of an object, but they can also return a value when the object is created. This article will introduce relevant knowledge about constructor return values in JavaScript.
Basic definition of constructor
A constructor is a special type of function that is used to create new objects. In JavaScript, constructors are named starting with a capital letter, which is one of the differences from ordinary functions. Constructors are usually used to initialize newly created objects and assign properties and methods to the objects. In the code below, we define a constructor called Person that creates a human object.
function Person(name, age) { this.name = name; this.age = age; this.sayHello = function() { console.log("Hello, my name is " + this.name + ", I am " + this.age + " years old."); } }
In the above code, Person is a constructor that accepts two parameters name and age, which is used to initialize a human object. The this keyword is used to point to the object currently being created. Through the this keyword, we can specify different attribute values and methods for each object. The attributes and methods in the object have their corresponding values. The attribute value can be any type of value, including numbers, strings, Boolean values, etc. The method is usually a function.
When creating a Person object, we can use the following method:
var person = new Person("Tom", 20);
This line of code creates an object named person, uses the Person constructor, and passes in name and age parameter. Next, we can call the sayHello method of the object:
person.sayHello(); // 输出:Hello, my name is Tom, I am 20 years old.
In the above code, we call the sayHello method of the person object and output some information.
Return value of constructor
In JavaScript, a constructor can have a return value, and the returned value can be of any type. If the constructor does not explicitly return a value, a new object is returned by default. If the constructor returns a normal value, the value is ignored and a new object is returned. If the constructor returns an object, that object replaces the original object that was created. The following code demonstrates the different situations of constructor return values:
function Cat(name, age) { this.name = name; this.age = age; this.sayHello = function() { console.log("Hello, my name is " + this.name + ", I am " + this.age + " years old."); } // 返回值为undefined } var cat1 = new Cat("Jack", 3); var cat2 = new Cat("Mimi", 4); console.log(cat1); // 输出:Cat {name: "Jack", age: 3, sayHello: ƒ} console.log(cat2); // 输出:Cat {name: "Mimi", age: 4, sayHello: ƒ}
For the above code, although the Cat constructor does not return a value at the end, two new objects cat1 and cat2 are still created and correctly Properties and methods are set for each object.
Next, let's look at an example of returning a normal value:
function Dog(name, age) { this.name = name; this.age = age; this.sayHello = function() { console.log("Hello, my name is " + this.name + ", I am " + this.age + " years old."); } return "This is a new dog."; // 返回一个字符串 } var dog1 = new Dog("Puppy", 1); var dog2 = new Dog("Teddy", 2); console.log(dog1); // 输出:Dog {name: "Puppy", age: 1, sayHello: ƒ} console.log(dog2); // 输出:Dog {name: "Teddy", age: 2, sayHello: ƒ}
When the constructor returns a string, it does not affect the creation of the object. In this case, the constructor return value is ignored and a new object is returned.
Finally, let's look at an example of returning an object:
function Car(model, year) { this.model = model; this.year = year; this.engine = { cylinders: 4, displacement: 2.0, horsepower: 200 }; return this.engine; // 返回一个对象 } var car1 = new Car("BMW", 2017); var car2 = new Car("Mercedes-Benz", 2018); console.log(car1); // 输出:{cylinders: 4, displacement: 2.0, horsepower: 200} console.log(car2); // 输出:{cylinders: 4, displacement: 2.0, horsepower: 200}
In the above example, the Car constructor returns the car1.engine object, thus creating a new object car1 with only the engine object. When the car2 object is created using the same constructor again, the same engine object is returned.
Summary
Through the above examples, we can see that in JavaScript, the constructor can have a return value, and the type of the return value can be any type, including undefined, ordinary values and objects . By default, the constructor returns a new object. If the constructor returns a non-undefined value (including null), the value will replace the original object. Understanding the rules for constructor return values can help us better understand and use the object-oriented programming features in JavaScript.
The above is the detailed content of How to return value from constructor in javascript. For more information, please follow other related articles on the PHP Chinese website!