J
jangchoe
I've heard that JavaScript is a prototype-based language instead of a
class based one. I'm interested in learning the prototype-based
paradigm of OO programming, but I am unsure of the best way to use
JavaScript as a prototype-based language. I'm stuck between two ways
and both methods might be right (or wrong).
Method 1:
/* create an Animal "object" */
function Animal() {} ;
/* "copy" the animal object to a new object mouse */
var mouse = new Animal();
/* create a method to the animal */
Animal.prototype.speak = function() { alert('squeak!'); };
mouse.speak(); /* alerts squeak! */
Method 1 doesn't really feel like it's true prototype-based because I
need to instantiate it using 'new' instead of cloning an object like
Io or Self. So I created a second method that just uses JavaScript
objects, which to me, feels a bit more prototype-based.
Method 2:
/* create an Animal object */
var Animal = {}; // same as var Animal = new Object();
/* "copy" the animal object to a new object mouse */
var mouse = Animal;
/* create a method to the animal */
Animal.speak = function() { alert('squeak!'); };
mouse.speak(); /* alerts squeak! */
There's also a method three which basically combines both methods.
function Animal() {};
var mouse = new Animal();
mouse.speak = function() { alert('squeak!'); };
mouse.speak();
But if I use method 3, I wouldn't know why I created an empty Animal
function if I was going to dynamically add new methods to the objects
it instantiated. So based on the 3 methods above (or an entirely new
method) which simulates prototype-based programming in JavaScript?
Thanks.
class based one. I'm interested in learning the prototype-based
paradigm of OO programming, but I am unsure of the best way to use
JavaScript as a prototype-based language. I'm stuck between two ways
and both methods might be right (or wrong).
Method 1:
/* create an Animal "object" */
function Animal() {} ;
/* "copy" the animal object to a new object mouse */
var mouse = new Animal();
/* create a method to the animal */
Animal.prototype.speak = function() { alert('squeak!'); };
mouse.speak(); /* alerts squeak! */
Method 1 doesn't really feel like it's true prototype-based because I
need to instantiate it using 'new' instead of cloning an object like
Io or Self. So I created a second method that just uses JavaScript
objects, which to me, feels a bit more prototype-based.
Method 2:
/* create an Animal object */
var Animal = {}; // same as var Animal = new Object();
/* "copy" the animal object to a new object mouse */
var mouse = Animal;
/* create a method to the animal */
Animal.speak = function() { alert('squeak!'); };
mouse.speak(); /* alerts squeak! */
There's also a method three which basically combines both methods.
function Animal() {};
var mouse = new Animal();
mouse.speak = function() { alert('squeak!'); };
mouse.speak();
But if I use method 3, I wouldn't know why I created an empty Animal
function if I was going to dynamically add new methods to the objects
it instantiated. So based on the 3 methods above (or an entirely new
method) which simulates prototype-based programming in JavaScript?
Thanks.