Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I'm not that into dynamic programming languages but I've written my fair share of JavaScript code. I never really got my head around this prototype-based programming, does anyone know how this works?

var obj = new Object(); 

obj.prototype.test = function() { 

alert('Hello?'); 

}; 

var obj2 = new obj(); 

obj2.test();

I remember a lot of discussions I had with people a while back (I'm not exactly sure what I'm doing) but as I understand it, there's no concept of a class. It's just an object, and instances of those objects are clones of the original, right?

But what is the exact purpose of this ".prototype" property in JavaScript? How does it relate to instantiating objects?

1 Answer

0 votes
by (106k points)

The prototype is the internal property of every JavaScript object. 

The prototype also allows to simulate classes in JavaScript, although JavaScript's inheritance system is - as we have seen - prototypical, and not class-based:

Methods are implemented the same way for each instance as in class-based systems, so methods are normally added to the prototype, whereas an object's fields are instance-specific and therefore added to the object itself during construction.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

Browse Categories

...