This article brings you three ways to create objects. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. .
Three ways to create objects:
1. Single mode
var xxx = {xxx:xxx,xxx:xxx}
(Recommended tutorial: js tutorial)
2. Prototype mode
Attributes are placed in the constructor
function Teacher(name,age){ this.name = name; this.age = age; } Teacher.prototype.showName = function(){ return this.name; } var xxx = new Teacher('xxx',23); xxx.showName();
3. Pseudo-class mode Class
class Teacher{ constructor(name,age){ this.name = name; this.age = age; } showName(){ return this.name; } } var xxx = new Teacher('xxx',23); xxx.showName();
For more programming related content, please pay attention to php Chinese website Introduction to Programming column!
The above is the detailed content of Three ways to create objects. For more information, please follow other related articles on the PHP Chinese website!