TIL(Object, Prototype)

JEHYUN KIM
2 min readDec 10, 2020

Earlier, I learned that object is a very broad thing. A lot of stuff is an object in JS.
For example, a function is also an object.
There are a few ways to create objects in JS.

We can create and define an object using an object literal.
An object literal is a list of name: value pairs (like hobby: basketball) inside curly brackets.

let cat = {name: "Jerry", species: Bengal, age: Two}

We can create and define an object using the keyword new.

let cat = new Object ();
cat.name = "Jerry";
cat.species = "Bengal";
cat.age = Two;

Or we can also use the constructor function.

function Cat(name, species, age) {
this.name = name;
this.species = species;
this.age = age;
}
let cat1 = new Cat("Jerry", "Bengal", "Two");
let cat2 = new Cat("Piper", "Brown tabby", "One");
  • ES5 also uses Object.create(). But now we are using ES6 or upper.
    This is used to create and return a new object with the specified prototype object and properties.

class keyword
As we can know from its name, the classis used to create a class.
A classalways starts with a capital letter(the first letter).

Prototype

JS is a language based on the prototype.
According to the dictionary, prototype means “a first, typical or preliminary model of something, especially a machine, from which other forms are developed or copied.”
Seems like the ‘class’ from JAVA and C++ are similar to the prototype of JS.

The prototype is used when we want to add new properties at a later stage to a function that is going to be shared across all the instances.
The prototype property is an object(= prototype object) where we can attach methods and properties in a prototype object. And this lets every other object to inherit the methods and properties.

__proto__
The __proto__ property of Object.prototypeis an accessor property ( a getter function and a setter function) that exposes the internal [[Prototype]] (either an object or null) of the object through which it is accessed.

Prototype chaining

let Cat = function(name) {
this.name = name;
}
Cat.prototype.purr = function() {};
let jerry = new Cat('jerry;);
jerry.toString(); // [object Object}

super keyword

super refers to the parent class. This is used to call the parent class’s constructor and to access its properties and methods.

--

--