2017-10-05 63 views
0

上午在JavaScript OOP新變化,請多多包涵值不是繼承通過原型

改變從繼承的對象學生父對象的值應該改變人的年齡,但我得到完全相同的值。

<script> 
function Person(age){ 
    this.age=age; 
} 

    function Student(){} 

    var person=Student.prototype=new Person(10); 
    var oldAge=person.age; 
    Student.age=20; 
    var newAge=person.age; 

    alert("Person old age="+oldAge+"\New age="+newAge); 
</script> 

personStudent來自同一Person對象繼承隨後的兩sudent和人的年齡值應改變從學生

改變價值,我已經通過Prototypical inheritance - writing upJavaScript Inherited Properties Default Value問題

問題是我想通過繼承Person屬性的Student來更改Person的值。

我想我錯過了一些東西,請幫我理解這一點。

+0

[不要使用Student.prototype = new Person(10);'!](https://stackoverflow.com/questions/12592913/what-is-the-reason-to-use-the-new-關鍵字 - 在這裏) – Bergi

+0

多數民衆贊成在問題@Bergi我想通過學生改變人的價值 – phpdroid

+0

那麼你應該[正確從'人'繼承'學生'](https://stackoverflow.com/questions/10898786/correct-javascript ),然後使用'var studentOne = new Student();的console.log(studentOne.age); studentOne.age = 20; ...' – Bergi

回答

2

有其中使用在JavaScript來實現繼承

  1. 原型面向對象的圖案
  2. 構造面向對象的圖案

現在,我將使用第一種方法兩個圖案

一些必備知識:

  1. 所有的JS對象都指向一個原型 對象的屬性,因此除了它本身的性質,目的也它自己的原型 訪問調性質

  2. __proto__:這是一個所有對象都擁有的屬性,這指向該對象的原型 。

  3. Object.create(arg):它用於創建對象和initaliaze 它們的原型或設置它們的__proto__屬性。

    Object.create MDN link

    以下片斷實現繼承,以及允許您通過學生改變人的價值。 :

function Person(age){ 
 
    this.age=age; 
 
    this.getAge = function() { return this.age;} 
 
}; 
 

 
function Student(){}; 
 

 
//Creating Person instance 
 
var person = new Person(23); 
 

 
console.log("Old Person age is " + person.age); 
 

 
//Creating a student instance and inheriting it from person instance 
 
//Object.create method creates a object whose __proto__ point to the object passed 
 
//Thus student will be an object having a property __proto__ that would point to person instance 
 
//This assosciation allows the instance of student to access insatnce of Person 
 
var student = Object.create(person); 
 

 
//Change age of person through student 
 
student.__proto__.age = 24; 
 

 
console.log("New Person age is " + person.age); 
 

 
console.log("We can also call parent object methods from child" + " for e.g calling getAge from student" + student.getAge());

現在,使用第二方法來實現類似的東西,下面的代碼片斷可以使用:

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

 
function Student(){} 
 

 
//Create person instance 
 
var person = new Person(23); 
 

 
console.log("Old age of person is " + person.age); 
 

 
//Inherit the person instance 
 
Student.prototype = person; 
 

 
//create a student object 
 
var student = new Student(); 
 

 
//Change the person instance age value 
 
//this change is possible because we 
 
//can access person object through 
 
//student.__proto__. 
 
student.__proto__.age = 24; 
 

 
console.log("New age of person is " + person.age);

+0

當我正確閱讀它時很有趣,您只需使用名稱'student',它與我的問題沒有多大關係! – phpdroid

+0

我覺得你因爲我最後的控制檯日誌而感到困惑,我在最後一個控制檯日誌中使用了學生,只是爲了演示子對象如何調用父對象的方法,順便提一下,我建議你再次檢查我的代碼並查看我的第一個和第二個控制檯日誌在那裏你可以看到我通過學生的實例修改了人的年齡,是不是你的原始問題? – varunsinghal65

+0

你已經創建'學生'變量不是'學生'對象你沒有使用任何地方'學生'僅限於函數'學生'是未觸及 – phpdroid

0

最簡單的解釋方法是person.age是一個實例的屬性,其中Student.age是一個與您的實例無關的靜態屬性。

您可以簡化整個示例以刪除學生,並且您仍會看到您擁有實例屬性和靜態屬性。

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

var person = new Person(10); 
var oldAge = person.age; 
Person.age = 20; 
var newAge = person.age; 

alert("Person old age=" + oldAge + "\New age=" + newAge); 
alert(Person.age); 
+0

問題是我想通過學生繼承人的財產來改變人的價值。 – phpdroid

0

在JavaScript中,你應該總是使用原型繼承,使這項工作。

var person = { 
    age: 10 
} 
var student = Object.create(person); 
var oldAge=person.age; 
student.age=20; 
var newAge=student.age; 

alert("Person old age="+oldAge+"\New age="+newAge); 

在代碼中,由於功能的學生沒有在創建階段的產權年限,JavaScript引擎將創建一個名爲年齡在內存性能。在執行階段,JavaScript引擎將爲創建階段創建的新屬性分配20個值。

如果你在瀏覽器內執行,你會注意到函數Student有一個新的屬性,稱爲age,等於20。

+0

所以我不能從Student對象中更改Person對象的值嗎? – phpdroid

+0

@phpdroid看到我的代碼片段,我已經完成了 – varunsinghal65