簡體   English   中英

如何在現有構造函數中添加新屬性作為參數

[英]How to add new property in existing constructor as a parameter

我創建了兩個構造函數Person和Employee。 Employee結構繼承了Person的屬性。 這工作正常。 現在,我想用原型在Person構造函數中添加新屬性。 但是得到空字符串。 請幫忙。

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());

演示小提琴

您可以嘗試以下實現:

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進行繼承(而不是實例)也更好,有關該細節的更多信息,請閱讀答案

更多不同上前為獲得原型答案

正如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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM