Click here to Skip to main content
15,890,946 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following js literal object prototype, and whenever I create multiple with Object.create, the methods of this object are also added to memory?

What I have tried:

JavaScript
edge_class = function( arg ){
    const name = arg;     
    return {
        display: function(){
            return name;
        },
        hide: function(){
             name = '';
        }
    }
}

var member = Object.create(edge_class('cat'));

When creating an object instance with Create.Object, does the function have to be defined separately as a prototype?

JavaScript
edge_class.prototype.display = function(){
    return this.name;
}
Posted
Updated 15-Jun-22 18:44pm

1 solution

Prototype works like a base class. Javascript is a dynamic language. You can add fields and functions add any point in time. Using the prototype ensures all instances to inherit the fields and functions defined in the prototype.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900