Category: JavaScript
-
Simplifying Object.assign method in JavaScript
Read full article on the Infragistics blog “In JavaScript, Object’s assign method copies source object’s own enumerable properties to a target object, and returns that target object “ There are two important keywords in the above sentence: Enumerable Own Before we go ahead and understand purpose of Object.assign, it is essential that we really understand these two words,…
-
Two Problems of a JavaScript Class
Starting ECMA 6, JavaScript has class keyword to create a class. I have written in detail about class here. There is no second question that class simplifies the way object is created, inheritance is implemented etc. JavaScript class has, Constructors Methods Extends etc. Above features of a class helps to easily write Object Oriented…
-
How to add a static member property in a JavaScript class?
Recently while solving a problem, I came across a requirement to create a property in a JavaScript class, which needs to be shared by all the object instances. In the programming world, these types of properties are called Static Properties. There are various scenarios when you need a static member property: When counting number of…
-
How to Create Basic Inheritance in JavaScript Constructors
There are four ways to create an object in JavaScript. They are as follows: Object as literal Constructor Invocation Pattern Object.create() method Using class after ES6 Implementation of Inheritance varies according to the object creation method. In this post, I am going to explain creating inheritance in between a function constructor. Let’s say you have…
-
How to seal, freeze and prevent extension of an object in JavaScript
Read full article on the Infragistics blog In modern JavaScript, objects are integral, and having a strong understanding of topics surrounding objects is necessary for writing better JavaScript. You can create an object in four ways in JavaScript. Read about them in greater detail here. Once you know how to create an object, you may…
-
Module Pattern in JavaScript
In JavaScript, code encapsulation can be achieved using Modules Patterns. In addition, it is used to create private and public properties. There are various ways a module pattern can be implemented. In this article, we will learn to create a module pattern in ES5. Before we go ahead and start looking into implementation of the…
-
How to Count the Number of Properties of the JavaScript Object
While working with JavaScript, I come across a requirement to count a number of properties in a JavaScript object. I found two ways to find the number of properties in an object. They are as follows: Using for loop Using Object.keys Consider an object, “cat”, as demonstrated below: This file contains bidirectional Unicode text that…
-
Simplifying function expressions and the function statement in JavaScript
Read full article on the Infragistics blog In JavaScript, a function can be created in three possible ways: Function as an expression Function as a statement Arrow functions In this post, we will learn about function expressions and the function statement. Consider the following code: This file contains bidirectional Unicode text that may be interpreted…
-
What are the Call and Apply Methods in JavaScript
Read full article on the Infragistics blog In JavaScript, the apply() and call() methods execute a function in the context (scope) of the first argument you pass to them. Let’s take a look at them in action to learn more. Say that you have an object person as shown below: This file contains bidirectional Unicode text that…
-
Four Ways to Create Objects in JavaScript
In JavaScript, there are four methods to use to create an object: Object literals New operator or constructor Object.create method Class In this post, we will learn each of these methods. Object literals An object literal, also called an object initializer, is a comma-separated set of paired names and values. You can create an object…