Home > Web Front-end > JS Tutorial > body text

Three ways to create objects

不言
Release: 2020-02-27 13:56:29
Original
7565 people have browsed it

Three ways to create objects

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}
Copy after login

(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();
Copy after login

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();
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!