在 JavaScript 中,构造函数用于创建和初始化对象。
这是一个构造函数的简单示例。有关更多信息,请阅读本教程的其余部分。
示例
// constructor function
function Person () {
this.name = "John",
this.age = 23
}
// create an object
const person = new Person();
// print object attributes
console.log(person.name);
console.log(person.age);
// Output:
// John
// 23
这里,Person()
是一个对象构造函数。我们使用 new
关键字从构造函数创建对象。
使用构造函数创建多个对象
在 JavaScript 中,您可以从构造函数创建多个对象。例如,
// constructor function
function Person () {
this.name = "John",
this.age = 23,
this.greet = function () {
console.log("hello");
}
}
// create objects
const person1 = new Person();
const person2 = new Person();
// access properties
console.log(person1.name); // John
console.log(person2.name); // John
在上面的程序中,我们使用相同的构造函数创建了两个对象(person1 和 person2)。
在 JavaScript 中,当 this
关键字用于构造函数时,this
指的是创建它的特定对象。例如,
// constructor function
function Person () {
this.name = "John"
}
// create object
const person1 = new Person();
// access properties
console.log(person1.name); // John
因此,当对象访问构造函数的 name 属性时,它可以直接访问,如 person1.name
。
JavaScript 构造函数参数
您也可以创建带参数的构造函数。例如,
// constructor function with parameters
function Person (person_name, person_age, person_gender) {
// assign parameter values to the calling object
this.name = person_name,
this.age = person_age,
this.gender = person_gender,
this.greet = function () {
return (`Hi ${this.name}`);
}
}
// create objects and pass arguments
const person1 = new Person("John", 23, "male");
const person2 = new Person("Sam", 25, "female");
// access properties
console.log(person1.name); // John
console.log(person2.name); // Sam
在上面的示例中,我们在创建对象时向构造函数传递了参数。
const person1 = new Person("John", 23, "male");
const person2 = new Person("Sam", 25, "female");
这使得每个对象都可以拥有不同的属性。
person1 | person2 |
---|---|
name 保存值 John 。 |
name 保存值 Sam 。 |
age 保存值 23。 | age 保存值 25。 |
gender 保存值 male 。 |
gender 保存值 female 。 |
构造函数与对象字面量
对象字面量用于创建单个对象。
另一方面,如果您想创建多个对象,构造函数会很有用。例如,
// use object literal to create
// a single object person
let person = {
name: "Sam"
}
// use constructor function
function Person () {
this.name = "Sam"
}
// create multiple objects
// from constructor function
let person1 = new Person();
let person2 = new Person();
从构造函数创建的对象是唯一的。因此,您可以向特定对象添加一个新属性,而该属性对其他对象不可用。例如,
// use constructor function
function Person () {
this.name = "Sam"
}
let person1 = new Person();
let person2 = new Person();
// add new property to person1
person1.age = 20;
// add a method to person1 object
person1.greet = function () {
return "hello";
}
console.log(person1.age); // 20
console.log(person1.greet()); // hello
console.log(person2.age); // undefined
console.log(person2.greet()); // ERROR!
在这里,age 属性和 greet()
方法是 person1 所特有的,因此 person2 无法访问。
另一方面,当使用对象字面量创建对象时,从该对象派生的任何对象变量都将充当原始对象的克隆。
因此,您在一个对象中所做的更改将在另一个对象中反映出来。例如,
// use object lateral
let person = {
name: "Sam"
}
console.log(person.name); // Sam
// assign person object to student variable
let student = person;
// change the property of student object
student.name = "John";
// add method to student object
student.greet = function () {
return "hello";
}
// original object property is also changed
console.log(person.name); // John
// original object now has the greet() method
console.log(person.greet()); // hello
JavaScript 内置构造函数
JavaScript 还具有内置构造函数来创建各种类型的对象。其中一些是
构造函数 | 描述 |
---|---|
Object() |
创建一个具有属性和方法的新对象。 |
String() |
构造一个字符串对象,用于处理和表示文本数据。 |
Number() |
构造一个数字对象,用于处理数据和运算。 |
Boolean() |
构造一个布尔对象,表示用于逻辑运算的 true 或 false 值。 |
示例:JavaScript 内置构造函数
// use Object() constructor to create object
const person = new Object({ name: "John", age: 30 });
// use String() constructor to create string object
const name = new String ("John");
// use Number() constructor to create number object
const number = new Number (57);
// use Boolean() constructor to create boolean object
const count = new Boolean(true);
console.log(person);
console.log(name);
console.log(number);
console.log(count);
输出
{ name: 'John', age: 30 } [String: 'John'] [Number: 57] [Boolean: true]
更多关于构造函数
您还可以使用原型向构造函数添加属性和方法。例如,
// constructor function
function Person () {
this.name = "John",
this.age = 23
}
// create objects
let person1 = new Person();
let person2 = new Person();
// add a new property to the constructor function
Person.prototype.gender = "Male";
console.log(person1.gender); // Male
console.log(person2.gender); // Male
要了解更多关于原型的信息,请访问JavaScript 原型。
在 JavaScript 中,class
关键字是在 ES6 (ES2015) 中引入的,它提供了更方便的语法来定义对象及其行为。
类作为创建具有相似属性和方法的对象的蓝图。它们类似于 JavaScript 中构造函数的功能。
要了解更多,请访问JavaScript Classes。
另请阅读