如何创建和使用Bash脚本

钟远东 / 2020-06-12

Jump to Content

Introduction

Objects in JavaScript are collections of key/value pairs. The values can consist of properties and methods, and may contain all other JavaScript data types, such as strings, numbers, and Booleans.

All objects in JavaScript descend from the parent Object constructor. Object has many useful built-in methods we can use and access to make working with individual objects easier. Unlike Array prototype methods like sort() and reverse() that are used on the array instance, Object methods are used directly on the Object constructor, and use the object instance as a parameter. This is known as a static method.

In order to get the most out of this tutorial, you should be familiar with creating, modifying, and working with objects, which you can review in the Understanding Objects in JavaScript article.

test

Object.create() is used to create a new object and link the prototype of an existing object. In Understanding Prototypes and Inheritance in JavaScript we learn how prototypes are used to link properties and functions between objects.

We can create a job object instance, and extend it to a more specific object.

// Initialize an object with properties and methods
const job = {
  position: "cashier",
  type: "hourly",
  isAvailable: true,
  showDetails() {
    const accepting = this.isAvailable
      ? "is accepting applications"
      : "is not currently accepting applications"

    console.log(
      `The ${this.position} position is ${this.type} and ${accepting}.`
    )
  },
}

// Use Object.create to pass properties
const barista = Object.create(job)

barista.position = "barista"
barista.showDetails()