如何在现有构造函数中添加新属性作为参数

时间:2015-04-13 08:25:22

标签: javascript oop

我创建了两个构造函数Person和Employee。 Employee构造继承Person的属性。这工作正常。现在我想在Person构造函数中添加带有prototype的新属性。但得到空白的字符串。请帮忙。

JS代码

function Person (age, weight) {
        this.age = age;
        this.weight = weight;
    }

    Person.prototype.name = name;

    //we will give Person the ability to share their information.
    Person.prototype.getInfo = function () {
        return "I am " + this.age + " year old and weight " + this.weight + " kg.";
    };


    function Employee (age, weight, salary, name) {

        //Person.call(this, age, weight);  // by call parameters as arguments
        Person.apply(this, arguments);  // by apply arguments as array
        this.salary = salary;
    }

    Employee.prototype = new Person();
    Employee.prototype.constructor = Employee;

    Employee.prototype.getInfo = function () {
        return "I am "+this.name+" " + this.age + " year old and weight " + this.weight + " kg having $" + this.salary +" pm salary";
    }


    var person = new Employee(30, 70, 4000, "Manish");
    console.log(person.getInfo());

    document.write(person.getInfo());

Demo Fiddle

2 个答案:

答案 0 :(得分:0)

你可以试试这个实现:

function Person (age, weight, name) {
    var  proto = this.getProto(this);
    this.age = age;
    this.weight = weight;
    if (proto) {
        proto.name = name;
    }
}

Person.prototype.getProto = function (instance) {
    var proto;
    if (Object.getPrototypeOf) {
        proto = Object.getPrototypeOf(instance);
    } else {
        proto = instance.__proto__;
    }
    return proto;
}

//we will give Person the ability to share their information.
Person.prototype.getInfo = function () {
    return "I am " + this.age + " year old and weight " + this.weight + " kg.";
};


function Employee (age, weight, salary, name) {

    Person.call(this, age, weight, name);  // by call parameters as arguments
    //Person.apply(this, arguments);  // by apply arguments as array
    this.salary = salary;
}

Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;

Employee.prototype.getInfo = function () {
    return "I am "+this.name+" " + this.age + " year old and weight " +
        this.weight + " kg having $" + this.salary +" pm salary";
}


var person = new Employee(30, 70, 4000, "Manish");
console.log(person.getInfo());

document.write(person.getInfo());

最好使用Object.create进行继承,而不是实例更多有关详细信息,请参阅answer

answer

中获得原型的更多不同之处

答案 1 :(得分:0)

根据 Pencroff 的建议,我使用以下方法完成了Object.create的要求。

function Person () {};
function Employee(){};

// helper function for Object.create()
Function.prototype.extends = function(superclass){
    this.prototype = Object.create(superclass.prototype);
    this.prototype.constructor = this;
}

Employee.extends(Person);

//Helper function for add methods in consturctor.
Function.prototype.methods = function(name, func){
    this.prototype[name] = func;
    return this;
}

Person
    .methods("name", function(name){ return name; })
    .methods("age", function(age){ return age;})
    .methods("weight", function(weight){ return weight;})
    .methods("getPersInfo", function(age, weight){
        var persAge = this.age(age),
            persWeight = this.weight(weight);
        return "I am " + persAge + " year old and weight " + persWeight + " kg.";
    });

Employee
    .methods("salary", function(salary){return salary;})
    .methods("getInfo", function(name, age, weight, salary){
        var empAge = this.age(age),
            empWeight = this.weight(weight),
            empName = this.salary(name),
            empSalary = this.salary(salary);

        return "Empoyee name is "+empName+ " age " + empAge + " year old and weight " + empWeight + "kg. having $"+empSalary+" in hand.";
    });



var person = new Person();
var employee = new Employee();

console.log(employee.getInfo("Mahesh", 30, 70, 4000));
document.write(employee.getInfo("Mahesh", 30, 70, 4000));