我是初学者,无法理解原型和继承在JavaScript中是如何工作的。我基于这里的代码: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Introduction_to_Object-Oriented_JavaScript#Inheritance 我无法弄清楚如何继承属性值,我只能得到“方法”。我认为也许更合适的问题是如何在调用子对象时启动父类的字段?JS中的继承,继承属性和值
按照提到的网站,我写了这样的事情:
function Person(name, surname) {
this.name = name;
this.surname = surname;
}
function Student(index) {
this.index = index;
Person.call(this);
}
Student.prototype = new Osoba();//I tried to insert values here in the constructor, it doesn't work
Student.prototype.constructor = Student;
var x = new Student ('89890');//tried to insert additional values here in the constructor, it also doesn't work
有没有一种方法来创建一个学生,给他也是一个姓名?
我是一个总noobie所以请解释,就像你会解释给一个5岁。 PS。我不得不这样做的JS,所以请不要推荐不同的方式,它不会帮助我,谢谢:)
也许下面的回答可以帮助你,也有与构造函数的参数有关系的模式,但你可以用在任何功能链:http://stackoverflow.com/a/16063711/1641941 – HMR