0

im trying make an factory in javascript , but when the page upload ,it does'nt let me inherit and play it , i did 4 classes that inherit from one super clas , but anytime i try to load the code it give me the error undefined obj , although im inserting the default value in the supper class -Pc- what should i do ?

function Pc(obj) {
    this.type = obj.type || "enter Type";
    this.company = obj.company || "enter Company";
    this.warrenty = obj.warrenty || "enter Warranty";
    this.model = obj.model || "enter model";
    this.link = obj.link || "http://" + obj.link || "input link";
    this.price = obj.price || "Enter Price";
    this.picture = obj.picture || "input Pic";
    this.number = obj.number || "input number";
}

function HardDrive(obj) {
    this.rotationalSpeed = obj.rotationalSpeed || "7200RPM";
    this.buffersize = obj.buffersize || "64MB";
    this.capacity = obj.capacity || "1TB";
    this.Interface = obj.Interface || "'SATA 6 Gb/s";
}
HardDrive.prototype = new Pc();

function Proccesor(obj) {
    this.core = obj.core || 4;
    this.cache = obj.cache || "4MB";
    this.speed = obj.speed;
    this.inTheCart = false;
}
Proccesor.prototype = new Pc();

function Memory(obj) {
    this.speed = obj.speed || "DDR3 1600";
    this.intrface = obj.intrface || "8GB";
    this.casLatency = obj.casLatency || '9';
    this.capacity = obj.Capacity || "8GB";
    this.inTheCart = false;
}
Memory.prototype = new Pc();

function MotherBoard(obj) {
    this.socket = obj.socket || "1115";
    this.connection = obj.connection || "Alot";
}
Memory.prototype = new Pc();

function ComputerFactory() {
    this.newPart = function(obj) {
        switch (obj.type.toLowerCase()) {
            case "hardrive":
                this.pcPart = HardDrive;
                break;
            case "proccesor":
                this.pcPart = Proccesor;
                break;
            case "memory":
                this.pcPart = Memory;
                break;
            case "motherboard":
                this.pcPart = MotherBoard;
                break;
        }
        return new this.pcPart(obj);
    }
}
var x = new ComputerFactory();
var hd1 = ComputerFactory.newPart({
    type: "hardrive",
    model: 'WD5000AAKX',
    buffersize: '16 MB',
    capacity: '500GB',
    link: "www.google.co.il",
    picture: 'WD500Gb.jpg',
    price: 230,
    number: 'hd1',
});
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Ilan
  • 9
  • 6

1 Answers1

0

I think you are getting an error because of the following line -

HardDrive.prototype = new Pc();

Your Pc constructor (method) is not equipped with handling no parameter cases. Hence you pass nothing to the Pc method, obj is undefined and obj.type cannot be accessed.

jaibatrik
  • 6,770
  • 9
  • 33
  • 62