samedi 28 février 2015

JavaScript Pseudoclasses Pattern


Hi i'm new to JavaScript and i'm studying Object Creation Patterns, in particular i'm focused on Pseudoclasses Pattern, so i wrote a few lines of code to check if i get the concept:



var Car = function (name, x, y) {
this.name = name;
this.x = x;
this.y = y;
};

Car.prototype.drive = function (byX, byY) {
this.x += byX;
this.y += byY;
};

var ferrari = new Car("Ferrari", 5, 5);
ferrari.drive(5, 5);

var ferrari_proto = Object.getPrototypeOf(ferrari);
var ferrari_proto_proto = Object.getPrototypeOf(ferrari_proto);
var ferrari_proto_proto_null = Object.getPrototypeOf(ferrari_proto_proto);

console.log(ferrari_proto); // Should be Function
console.log(ferrari_proto_proto); // Should be Object
console.log(ferrari_proto_proto_null); // Should be Null


What i got from running the code is:



{ drive: [Function] }
{ }
null


and logging the type of these objects i got:



object
object
object


Now, what i thought was that creating objects this way, the ferrari prototype would have been the Car function, so what i expected was:



function // Tha Car function
object // The Function prototype, that is Object
object // null, that is the end of the chain


Someone can explain me why i got these outputs and why i was wrong ?!





Aucun commentaire:

Enregistrer un commentaire