2014-02-25 60 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接收一個對象並返回其原型是傳遞的對象的另一個對象。