let a = Object.create(Set.prototype);
The Object.create()
method creates a new object with the specified prototype object and properties.
In our case, Object.create()
creates a new object of new Set()
and it will inherits all properties and methods of Set instance.
The Set object lets you store unique values of any type, whether primitive values or object references.
With the help of argument Set.prototype
You can use the constructor's prototype object to add properties or methods to all Set instances.
a instanceof Set
It confirms that instance a
is a instance of Set
Object, and in our case yes it is.
a.add(1);
a
object has no method add()
You call the method without implements
Set.prototype.add() or
a.add()
In this both cases you can try to call add()
method via object instance a
, and a
object has no add()
method.
add()
method is related to Set
Class.
var oSet = new Set();
var CustomSet = Object.create(oSet);
console.log(CustomSet.__proto__.add(10));
When you run above code in your console, you will see it will push value 10 in set.
__proto__
is the actual object that is used in the lookup chain to resolve methods, etc.
prototype is the object that is used to build __proto__
when you create an object with new
So prototype is not available on the instances themselves (or other objects), but only on the constructor functions.