Logo

dev-resources.site

for different kinds of informations.

TOP - Objects and Object Constructors

Published at
11/8/2023
Categories
javascript
top
hdtop
Author
haridulai
Categories
3 categories in total
javascript
open
top
open
hdtop
open
Author
9 person written this
haridulai
open
TOP - Objects and Object Constructors

TOP

Introduction

There are multiple ways to define objects but in most cases, it is best to use the object literal syntax as follows:

const myObject = {
  property: 'Value!',
  otherProperty: 77,
  "obnoxious property": function() {
    // do stuff!
  }
};
Enter fullscreen mode Exit fullscreen mode

There are also 2 ways to get information out of an object:

  1. dot notation myObject.property; // 'Value!'
  2. square bracket notation myObject["obnoxious property"]; // [Function]

Which method you use will depend on context. Dot notation is cleaner and is usually preferred

you cannot use variables in dot notation:

const variable = 'property';

myObject.variable; // this gives us 'undefined' because it's looking for a property named 'variable' in our object

myObject[variable]; // this is equivalent to myObject['property'] and
Enter fullscreen mode Exit fullscreen mode

Lesson overview

This section contains a general overview of topics that you will learn in this lesson.

  • How to write an object constructor and instantiate the object.
  • Describe what a prototype is and how it can be used.
  • Explain prototypal inheritance.
  • Understand the basic do’s and don’t’s of prototypal inheritance.
  • Explain what Object.create does.
  • Explain what the this keyword is.

Object constructors

When you have a specific type of object that you need to duplicate like our player or inventory items, a better way to create them is using an object constructor, which is a function that looks like this:

function Player(name, marker) {
  this.name = name;
  this.marker = marker;
}
Enter fullscreen mode Exit fullscreen mode

and which you use by calling the function with the keyword new.

const player = new Player('steve', 'X');
console.log(player.name); // 'steve'
Enter fullscreen mode Exit fullscreen mode

Just like with objects created using the Object Literal method, you can add functions to the object:

function Player(name, marker) {
  this.name = name;
  this.marker = marker;
  this.sayName = function() {
    console.log(name)
  };
}

const player1 = new Player('steve', 'X');
const player2 = new Player('also steve', 'O');
player1.sayName(); // logs 'steve'
player2.sayName(); // logs 'also steve'

Enter fullscreen mode Exit fullscreen mode

Exercise

  • Write a constructor for making β€œBook” objects
  • Your book objects should have the book's title, author and number of pages, and whether or not you have read the book
  • Put a function into the constructor that can report the book info like so:
theHobbit.info(); // "The Hobbit by J.R.R. Tolkien, 295 pages, not read yet"

Enter fullscreen mode Exit fullscreen mode

The prototype

top Article's
30 articles in total
Favicon
Top 10 Cybersecurity Companies in India 2025
Favicon
10 Greatest Russian Soccer Players in History
Favicon
Unboxing: Seven & Me
Favicon
Why the Linux `top` Command Might Not Display All Processes: Understanding the Limitations
Favicon
Star packers and movers
Favicon
Dr. Stephen Nkem Asek Awarded Doctor Of Social Sciences (Honoris Causa) By AUBSS And QAHE
Favicon
The Best Windows Keyboard Shortcuts
Favicon
Economical Eats: Pocket-Friendly Breakfast in Amsterdam
Favicon
The Top Digital Marketing Agency in Cairo You Need to Know About
Favicon
Top Custom eLearning Development Companies in 2024
Favicon
Best Document Redaction APIs in 2023
Favicon
TOP - Objects and Object Constructors
Favicon
Industrial Drum Top Vacuums: Benefits and Applications - SPILVAC
Favicon
Top 100 Developers per Country - Exploring Stack Exchange Data Explorer
Favicon
Process management in GNU/Linux
Favicon
TOP 5 WEBSITES TO LEARN SQL
Favicon
Top 20+ Free and Premium Bootstrap Dashboard Templates to Consider in 2023
Favicon
Top 5 Data Science Companies In The World | 2022-23
Favicon
Top 10 full-cycle development companies in 2022
Favicon
Top IoT development companies you need to know in 2022
Favicon
Basic Guide in Estimating Project Size by Using Story Points
Favicon
Transform Android to Linux
Favicon
5 Reasons To Choose PHP Web Development For Business Website Development
Favicon
My top 10 best vs code extensions for frontend developers
Favicon
Top ways to become a self taught developer
Favicon
Today I have attempted 5 java programming exercises 😊.
Favicon
πŸ”Top 10 C# Projects on GitHub (Q1 2021) and the trendπŸ”₯πŸ”₯πŸ”₯
Favicon
Top Productivity Apps for 2021
Favicon
Top 5 CSS methodologies in 2021
Favicon
What is React Native?

Featured ones: