我可以在 js 中创建一个 object 槽 parent
Can i create an object trough parent in js
我是 OOJS 的新手,我在尝试理解继承时有点困惑,我创建了两个简单的 类,继承自 person 的 person 和 student,是否有通过传递数据创建 student 的选项在 parent 的构造函数中?如果可能的话,怎么做? child 可以从 parent 获取所有属性和方法还是仅获取方法?
**警报中的 fname 和 lName 未定义
function Person(fNme, lName) {
this.fname = fNme;
this.lName = lName;
}
Object.prototype.go = function() {
alert("I am going now last time you see "+ this.lName);
}
function Student() {
this.study = function () {
alert("I am studing !");
}
}
Student.prototype = new Person();
var s1 = new Student("sam", "bubu");
alert(s1.fname +"+"+ s1.lName)
你可以使用构造函数窃取。
function Student(fName,lName) {
Person.call(this,fName,lName);
this.study = function () {
alert("I am studing !");
}
}
当您调用 Student
构造函数时,您可以将参数传递给 Person()
和 call()
以初始化 Person
中的变量
这只需调用父构造函数即可完成
function Student(fName, lName, whateverelse) {
Person.call( this, fName, lName ); // call base class constructor function
this.study = function () {
alert("I am studing !");
}
}
我是 OOJS 的新手,我在尝试理解继承时有点困惑,我创建了两个简单的 类,继承自 person 的 person 和 student,是否有通过传递数据创建 student 的选项在 parent 的构造函数中?如果可能的话,怎么做? child 可以从 parent 获取所有属性和方法还是仅获取方法?
**警报中的 fname 和 lName 未定义
function Person(fNme, lName) {
this.fname = fNme;
this.lName = lName;
}
Object.prototype.go = function() {
alert("I am going now last time you see "+ this.lName);
}
function Student() {
this.study = function () {
alert("I am studing !");
}
}
Student.prototype = new Person();
var s1 = new Student("sam", "bubu");
alert(s1.fname +"+"+ s1.lName)
你可以使用构造函数窃取。
function Student(fName,lName) {
Person.call(this,fName,lName);
this.study = function () {
alert("I am studing !");
}
}
当您调用 Student
构造函数时,您可以将参数传递给 Person()
和 call()
以初始化 Person
这只需调用父构造函数即可完成
function Student(fName, lName, whateverelse) {
Person.call( this, fName, lName ); // call base class constructor function
this.study = function () {
alert("I am studing !");
}
}