Table of Contents
1. What is object-oriented programming?
2. The purpose of object-oriented programming
3. Advantages of object-oriented programming (inheritance, polymorphism, encapsulation)
How to implement object-oriented programming in JS (reference)
1. Prototype chain inheritance
2. Constructor inheritance
3. Combined inheritance
4. Parasitic combined inheritance
5, class implementation inheritance (reference)
Home Web Front-end JS Tutorial How does JS implement object-oriented programming? Introduction to the principles of js object-oriented programming

How does JS implement object-oriented programming? Introduction to the principles of js object-oriented programming

Aug 18, 2018 pm 04:53 PM
html javascript node.js

The content of this article is about how JS implements object-oriented programming? The introduction to the principles of js object-oriented programming has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. What is object-oriented programming?

It is an abstract way to create a programming model based on a real-world model (combining data and program instructions into objects)

2. The purpose of object-oriented programming

is to promote better flexibility and maintainability in programming, and is widely popular in large-scale software projects.

3. Advantages of object-oriented programming (inheritance, polymorphism, encapsulation)

Inheritance: Get all the data and functions of the parent class, and achieve copying.
Polymorphism: The same method name has different behaviors depending on the object that implements the method.
Encapsulation: Aggregate object data and functions, and limit their contact with the outside world (access rights).

How to implement object-oriented programming in JS (reference)

1. Prototype chain inheritance

function Person() {
    this.name = 'per'
    this.obj = {
        name: ''
    }
}
Person.prototype.getName = function() {
    return this.obj.name
}
Person.prototype.setName = function(name) {
    this.name = name
    // 引用类型的赋值会同步给所有子类
    this.obj.name = name
}
function Student() {
    
}
Student.prototype = new Person()

const stu1 = new Student()
const stu2 = new Student()

stu1.setName('stu')
stu1.getName()
stu2.getName()
Copy after login
Disadvantage: When the reference type is modified, it will be synchronized to all subclasses

2. Constructor inheritance

function Person() {
    this.obj = {
        name: 'a'
    }
    this.setName = name => {
        this.obj.name = name
    }
    this.getName = () => {
        return this.obj.name
    }
}
function Student() {
    Person.call(this)
}
const stu1 = new Student()
const stu2 = new Student()
stu1.setName('stu')
stu1.getName()
stu2.getName()
Copy after login
Disadvantages: The functions of the parent class are not shared under the subclass, which is equivalent to dynamically copying a copy of the code

3. Combined inheritance

function Person() {
    this.obj = {
        name: 'a'
    }
}
Person.prototype.getName = function() {
    return this.obj.name
}
Person.prototype.setName = function(name) {
    this.name = name
    // 引用类型的赋值会同步给所有子类
    this.obj.name = name
}
function Student() {
    // 继承属性
    Person.call(this)
}
// 继承方法
Student.prototype = new Person()
Copy after login
Disadvantages: The attribute copy in the parent class is executed twice

4. Parasitic combined inheritance

function Person() {
    this.obj = {
        name: 'a'
    }
}
Person.prototype.getName = function() {
    return this.obj.name
}
Person.prototype.setName = function(name) {
    this.name = name
    // 引用类型的赋值会同步给所有子类
    this.obj.name = name
}
function Student() {
    // 继承属性
    Person.call(this)
}
// 这里实现方法的继承
function inherit(sub, parent) {
    sub.prototype = Object.create(parent.prototype)
    sub.prototype.constructor = sub       
}
inherit(Student, Person)
Copy after login
Here is the solution to the double inheritance of the parent class code in combined inheritance Execution issues

5, class implementation inheritance (reference)

class Person {
    constructor(){
        this.obj = {
            name: 'a'
        }
    }
    get name() {
        return this.obj.name
    }
    set name(name) {
        this.obj.name = name
    }
}
class Student extends Person {
    constructor() {
        super()
    }
}
Copy after login

Related recommendations:

js object-oriented Summary of multiple methods of creating objects_js-oriented Object

javascript object-oriented programming code_js object-oriented

The above is the detailed content of How does JS implement object-oriented programming? Introduction to the principles of js object-oriented programming. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Table Border in HTML Table Border in HTML Sep 04, 2024 pm 04:49 PM

Guide to Table Border in HTML. Here we discuss multiple ways for defining table-border with examples of the Table Border in HTML.

HTML margin-left HTML margin-left Sep 04, 2024 pm 04:48 PM

Guide to HTML margin-left. Here we discuss a brief overview on HTML margin-left and its Examples along with its Code Implementation.

Nested Table in HTML Nested Table in HTML Sep 04, 2024 pm 04:49 PM

This is a guide to Nested Table in HTML. Here we discuss how to create a table within the table along with the respective examples.

HTML Table Layout HTML Table Layout Sep 04, 2024 pm 04:54 PM

Guide to HTML Table Layout. Here we discuss the Values of HTML Table Layout along with the examples and outputs n detail.

HTML Input Placeholder HTML Input Placeholder Sep 04, 2024 pm 04:54 PM

Guide to HTML Input Placeholder. Here we discuss the Examples of HTML Input Placeholder along with the codes and outputs.

Moving Text in HTML Moving Text in HTML Sep 04, 2024 pm 04:45 PM

Guide to Moving Text in HTML. Here we discuss an introduction, how marquee tag work with syntax and examples to implement.

HTML Ordered List HTML Ordered List Sep 04, 2024 pm 04:43 PM

Guide to the HTML Ordered List. Here we also discuss introduction of HTML Ordered list and types along with their example respectively

HTML onclick Button HTML onclick Button Sep 04, 2024 pm 04:49 PM

Guide to HTML onclick Button. Here we discuss their introduction, working, examples and onclick Event in various events respectively.

See all articles