Three Ways to Create Objects in JavaScript

Almost everything in Javascript is an object, whether it’s an array or a function. In this post, we’ll learn three different ways to create objects in JavaScript:

  1. Object Literals.
  2. New Keyword.
  3. Classes.
You may also like: Object-Oriented JavaScript: Objects, Encapsulation, and Abstraction (With Examples).

Object Literals

A JavaScript object literal is a comma-separated list of name-value pairs wrapped in curly braces. Object literals encapsulate data, enclosing it in a tidy package.

Kotlin Primitive and Object Arrays

I initially set out to write this post because I was playing around with some reflection code and thought I found something interesting. Alas, that was definitely not the case. Instead, it was just a basic feature of Kotlin that I haven’t needed to use or focus on yet. Although this post didn’t turn out the way I wanted it to, I still think it is a nice little post to bring some clarity to this subject.

In Java, there is the concept of primitive types and their wrapped versions. Thanks to autoboxing and unboxing, types can be interchanged between their primitive and wrapped versions. In other words, in most situations, you can use a long instead of a Long or a Long instead of a long. If you didn’t notice where the capitals were in that last sentence, then I imagine it probably looked quite confusing. The wording in that sentence is also crucial. More specifically, the statement “in most situations.”

Object Modeling Best Practices

OOP Is All About Encapsulation

Yes, that’s right. Object-oriented programming aims to encapsulate properties and methods into a consolidated object so that operations can be carried out on the object. The whole aim is to move away from procedural functions, which are not easy to reason with or prove correctness. But this principle often gets violated and people write procedural code using objects. Here is a classic example:

class Rectangle {
    private Long length;
    private Long breadth;

    //gettters
    //setters
    //constructors
}


Optimizing Memory Access With CPU Cache

Nowadays, developers pay less attention to performance because hardware has become cheaper and stronger. If developers understand some basic knowledge about CPU and memory, they can avoid simple mistakes and it is easy to improve the performance of their code.

At the end of this article, I also ask a question that I do not know the answer to, so any suggestions are welcome!

Exploding 3D Objects with Three.js

Today we’d like to share an exploding object experiment with you. The effect is inspired by Kubrick Life Website: 3D Motion. No icosahedrons were hurt during these experiments!

The following short walk-through assumes that you are familiar with some WebGL and shaders.

The demo is kindly sponsored by Airtable: Build MVPs faster than ever before. If you would like to sponsor one of our demos, find out more here.

How it’s done

For this effect we need to break apart the respective object and calculate all fragments.

The easiest way to produce naturally looking fragments, is to look at how nature does them:

giraffe

Giraffes have been using those fashionable fragments for millions of years.

This kind of pattern is called a Voronoi diagram (after Georgy Feodosevich Voronoy, mathematician).

voronoi
Image by Mike Bostock done with Voronator

We are lucky to have algorithms that can create those diagrams programmatically. Not only on surfaces, as giraffes do, but also as spatial ones that break down volumes. We can even partition four dimensional space. But let’s stop at three dimensions for today’s example. I will leave the four dimensional explosions as an exercise for the reader 😉

We prepared some models (you could use Blender/Cinema4D for that, or your own Voronoi algorithm):

heart
You can see that this heart is no longer whole. This heart is broken. With Voronoi.

That looks already beautiful by itself, doesn’t it? ❤

On the other hand, that’s a lot of data to load, so I managed to compress it with the glTF file format using Draco 3D data compression.

Shader

I decided to use three.js for the rendering, as it has a lot of useful built-in stuff. It’s great if you want reflecting materials, and it has some utilities for working with fragments and lightning.

With too many fragments it is not very wise to put all calculations on the CPU, so it’s better to animate that in the shaders, i.e. on the GPU. There’s a really simple vertex shader to tear all those fragments apart:

	position = rotate(position);
	position += position + direction*progress;

…where direction is the explosion direction and progress is the animation progress.

We can then use some three.js materials and CubeTexture to color all the surfaces, and that’s basically it!

During development, I accidentally typed the wrong variable in one of my shaders, and got pretty interesting result:

error

So, don’t be afraid to make mistakes, you never know what you end up with when you try something new!

I hope you like the demos and the short insight into how it works, and that this story will inspire you to do more cool things, too! Let me know what you think, and what ideas you have!

References and Credits

Exploding 3D Objects with Three.js was written by Yuriy Artyukh and published on Codrops.

NuGet Map Object Tool

In this article, we will talk about my first NuGet which map objects and their properties from one type to another.

I uploaded my first Mapper NuGet to Nuget's website; you can find it here. It's designed to help developers map object properties from one type to another without adding complex lines of code.