Will JS and JQ back-end development, how happy to turn the front-end: Part II – Section I: ES6 – object-oriented – play OOP flower – constructor and prototype

Time:2021-4-16

Chapter 2: ES6 object oriented

The introduction consists of three sections and three articles

1. Constructors and prototypes

2. Inheritance

3. New methods in Es5

Creating objects with constructors

  1. Using new object() to create objects

var obj1 = new Object();

  1. Creating objects with literal quantity of objects

var obj2 = {};

  1. Creating objects with constructors
function Star(uname, age) {
    this.uname = uname;
    this.age = age;
    this.sing = function() {
        console.log ('I can sing ');

        }
    }
  1. Call demo
Var DMM = new star ('Big power ', 18);
Var Bobo = new star ('bobo ', 19);
console.log(dmm);
dmm.sing();
bobo.sing();

result:

Will JS and JQ back-end development, how happy to turn the front-end: Part II - Section I: ES6 - object-oriented - play OOP flower - constructor and prototype

Member properties and member methods in constructors

The properties and methods in the constructor are called members, which can be added

function Star(uname, age) {
    this.uname = uname;
    this.age = age;
    this.sing = function() {
        console.log ('I can sing ');

        }
    }

How to access instance members

Instance members can only be accessed through instantiated objects

var bo = new Star('bo', 18);

//1. An instance member is a member added through this in the constructor. Uname age sing is an instance member

console.log(bo.uname);

bo.sing();

//  console.log ( Star.uname ); // instance members cannot be accessed through constructors

PS: instance members cannot be accessed through constructors

Static members

A static member is a member added to the constructor itself

as followssexIt’s a static member

Star.sex  ='male ';

Access to static members

Static members can only be accessed through constructors

console.log(Star.sex);
console.log ( bo.sex ); // cannot be accessed through an object

This work adoptsCC agreementReprint must indicate the author and the link of this article

Thank you for your attention

Recommended Today

Review of SQL Sever basic command

catalogue preface Installation of virtual machine Commands and operations Basic command syntax Case sensitive SQL keyword and function name Column and Index Names alias Too long to see? Space Database connection Connection of SSMS Connection of command line Database operation establish delete constraint integrity constraint Common constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT […]