Learning JavaScript: Computing with Object Methods

Learning JavaScript: Computing with Object Methods

JavaScript has a set of built-in methods you can use with your user-defined objects. In this article I’m going to discuss several of these methods and how you can use them in your JavaScript programs.

JavaScript has a set of built-in methods you can use with your user-defined objects. In this article I’m going to discuss several of these methods and how you can use them in your JavaScript programs.

Object.assign

The Object.assign method is used to make a copy of one object into another object. The syntax template for this method is:

Object.assign(target, source);

where source is the object you are copying from and target is the object you are copying into. This method returns the target object if you want to assign it.

Here is a sample program that demonstrates how to use Object.assign:

function Student(name, id, grades) {
  this.name = name;
  this.id = id;
  this.grades = grades;
}
let st1 = new Student("",0,[]);
et st2 = new Student("Jane Doe", 123, [91, 92, 93]);
Object.assign(st1, st2);
print(`${st1.name}, ${st1.id}\n[${st1.grades}]`);

The output from this program is:

Jane Doe, 123
[91, 92, 93]

A good reason to use this method is to make sure that a new object has all the properties and values of the old object. You may accidentally leave something out when writing your own method, while Object.assign will systematically make sure all properties and values are assigned to the new object.

Object.create

The Object.create method creates a new object from an existing object prototype. Here is the syntax template for this method:

const|let|var object-name = Object.create(existing-object);

Let’s look at a few examples to see how this method works in practice. The first example creates a new object from a function and then creates a second object using Object.create:

function Student(name, id, grades) {
  this.name = name;
  this.id = id;
  this.grades = grades;
}
let st1 = new Student("Bob Green", 1234, [81, 77, 92]);
print(`${st1.name}, ${st1.id}\n${st1.grades}`);
let st2 = Object.create(st1);
print(`${st2.name}, ${st2.id}\n${st2.grades}`);

The output from this program is:

Bob Green, 1234
81,77,92
Bob Green, 1234
81,77,92

Code must be written to change the properties of the newly created object.

learn-to-code javascript-development learn-to-program javascript learning-javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Learning JavaScript: Development Environments for JavaScript Programming

One of the nice things about learning JavaScript these days is that there is a plethora of choices for writing and running JavaScript code. In this article, I’m going to describe a few of these environments and show you the environment I’ll be using in this series of articles.

Learning JavaScript: Working with Objects — this, getters, and setters

JavaScript objects provide the reader with some special features that make object-based programmer more effective and more efficient. In this article I’m going to describe three of these features — the this keyword, getters, and setters. The this Keyword If there was ever a more confusing keyword in computer programming than thethiskeyword, I don’t know what it is.

Learning to Code: How to Boost Up the Process?

Learning to Code: How to Boost Up the Process? I also often recommend different online and offline resources to my students to make their learning process easier, more effective, and faster. And in this post, I will share a few tips with you.

Learning JavaScript: for Loops

In this article I’m going to cover the different forms of the for loop that are available in JavaScript. There is the general for loop, the for..in loop, and the for..of loop. I will describe how each loop works and when is the right time to use each loop type. I am leaving out the Array.forEach loop as it is specialized for arrays and requires some knowledge of functions I haven’t covered yet.

13 Free/Low-Cost Sites to Supercharge Your Programming Self-Education

Although we still talk about programming as a standalone career, the dominance of technology in our lives makes it clear that coding is much more than a career path. In my opinion, computer science is more than a college major or a high-paid job; it’s a skill, essential for thriving in a modern-day economy. Whether you work in healthcare, marketing, business, or other fields, you will see more coding and have to deal with a growing number of technologies throughout your entire life.