2014-02-25 58 views
2

我正在研究在JavaScript中应用面向对象的方法。
我发现了一个使用继承的解决方案,我想知道是否有更好的方法以及如何包装我的类。javascript中的面向对象继承

这就是我迄今为止所做的。

People = function (name) { 
    this.name = name 
    this.age = null; 
}; 

Employee = function (name) { 
    People.call(this, name); 
    this.IdentificationCode = null; 
    this.salary = null; 
} 

Jonh = new Employee("Jonh Smith"); 
Jonh.age = 25; 
Jonh.IdentificationCode = 35632; 
Jonh.salary = 3500; 
+3

这个问题似乎是脱离主题,因为它是关于工作代码,它应该被迁移到http://codereview.stackexchange.com/ – Pavlo

+0

调用部分运行实例特定部分的继承。您可以通过原型继承行为更多信息请阅读以下答案.http://stackoverflow.com/a/16063711/1641941 – HMR

回答

3

注:你是不是从People继承,但你重用People的构造函数。

建议1:

确保你没有创建全局变量。

var People = function (name) {  // var at the beginning is important 
... 
... 
var Employee = function (name) { // var at the beginning is important 
... 
... 
var Jonh = new Employee("Jonh Smith"); 

建议2:

的构造函数应该有一个方法来初始化其他变量也是如此。

var People = function (name, age) { 
    this.name = name || null; 
    this.age = age || null; 
}; 
var Employee = function (name, age, idCode, salary) { 
    People.call(this, name, age); 
    this.IdentificationCode = idCode || null; 
    this.salary = salary || null; 
} 

由于People在其原型中没有任何方法,所以应该没问题。

但是,如果你有People的原型方法,你希望他们能提供给您的派生对象还有,你能做到这一点

var People = function (name, age) { 
    this.name = name || null; 
    this.age = age || null; 
}; 

People.prototype.getData = function() { 
    return [this.name, this.age]; 
}; 

现在定义Employee这样

var Employee = function (name, age, idCode, salary) { 
    People.call(this, name, age); 
    this.IdentificationCode = idCode; 
    this.salary = salary; 
} 

// Make the Employee's prototype an object of parent class's prototype 
Employee.prototype = Object.create(People.prototype); 

然后做,

var Jonh = new Employee("Jonh Smith", 25, 35632, 3500); 
console.log(Jonh.getData()); 
现在210

,它会调用PeoplegetData并将打印

[ 'Jonh Smith', 25 ] 

注:这种类型的继承通常被称为原型继承。

+0

最好不要创建父实例来设置继承的原型部分 – HMR

+0

@HMR Employee.prototype =的Object.create(People.prototype);'? – thefourtheye

+0

是的,现在这是一个很好的例子。 – HMR

0

你可以用很多方式做到这一点。一个是你所做的一样。 另一种是使用原型对象:

Employee.prototype = new People(); 

您也可以使用返回新创建的对象的功能,并调用一个从另一个:

function getPeople (name) { 
    var result; 
    result.name = name; 
    result.age = null; 

    return result; 
} 

function getEmployee (name) { 
    var result = getPeople (name); 
    result.IdentificationCode = null; 
    result.salary = null; 

    return result; 
} 
1

您可以设置Employee使用Object.createPeople继承。

var People = function (name) { 
    this.name = name 
    this.age = null; 
}; 

var Employee = function (name) { 
    People.call(this, name); 
    this.IdentificationCode = null; 
    this.salary = null; 
} 

Employee.prototype = Object.create(People.prototype); // create a new object inheriting from People.prototype 
Employee.prototype.constructor = Employee; // put the constructor back 

var Jonh = new Employee("Jonh Smith"); 
Jonh.age = 25; 
Jonh.IdentificationCode = 35632; 
Jonh.salary = 3500; 
2
function Person(name, age) { 
    this.name = name; 
    this.age = age; 
} 


function Employee(name, age) { 
    Person.call(this, name, age); 
    this.salary = null; 
} 


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

var teste = new Employee("Felipe",25) 
teste instanceof Employee // true 
teste instanceof Person // true 

的Object.create正在传承。 Object.create接收一个对象并返回其原型是传递的对象的另一个对象。