【问题标题】:trying to make inheritnce with factory and it doesnt inherit and give some error [duplicate]试图与工厂进行继承,它不会继承并给出一些错误[重复]
【发布时间】:2015-01-09 08:38:33
【问题描述】:

我正在尝试在 javascript 中创建工厂,但是当页面上传时,它不允许我 继承和播放它,我做了4个继承自一个超级类的类,但是任何时候 我尝试加载它给我错误 undefined obj 的代码,尽管我插入了 超类-Pc-中的默认值 我该怎么办?

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',
});

【问题讨论】:

    标签: javascript


    【解决方案1】:

    我认为您因为以下行而遇到错误 -

    HardDrive.prototype = new Pc();
    

    您的 PC 构造函数(方法)没有配备处理无参数情况。因此,您不向 Pc 方法传递任何内容,obj 未定义且无法访问 obj.type。

    【讨论】:

    • 谢谢,但它不能用那个,我以前和现在都试过了
    猜你喜欢
    • 2022-01-19
    • 2019-04-03
    • 2023-04-08
    • 2020-10-25
    • 2014-05-03
    • 2018-01-02
    • 1970-01-01
    • 2012-03-26
    • 2019-01-12
    相关资源
    最近更新 更多