Home >Web Front-end >JS Tutorial >Three ways to create objects

Three ways to create objects

不言
不言Original
2018-09-14 18:02:337648browse

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}

(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!

Statement:
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

Related articles

See more