我正在探索JavaScript中的遗产概念,我认为我错过了一些东西。
我的目标:我想创建一个继承自另一个对象的对象。我认为我错过了一些东西。
作为一个例子,我创建了一个对象"学生"继承自对象" Personne"。
我可以通过两种方式实现这一目标:
从我的观点来看,两种方式都很好。我的意思是:显然,无论哪种方式都可以。
但是,如果我看第二种方式,那么我注意到该对象没有原型。因此,我认为:这是正常的吗?事实上,我更喜欢第二种方式,因为我觉得它更优雅。
注意:我提供了一个可以与NodeJs一起使用的准备好的脚本。我也给出了执行结果。
所以我的问题是:
由于
// ---------------------------------------------
// We define a Personne.
// ---------------------------------------------
var Personne = function(inName) { // This is the constructor.
// Do some initialization.
console.log("Executing the constructor Personne.");
if ('undefined' != typeof inName) {
this.name = inName;
}
}
var PersonnePrototype = { // This is the prototype.
name: undefined,
setName: function(inName) { this.name = inName; },
getName: function() { return this.name; }
};
Personne.prototype = PersonnePrototype;
// ---------------------------------------------
// We define a Student.
// ---------------------------------------------
var Student = function(inAge, inName) { // This is the constructor.
// Do some initialization.
console.log("Executing the constructor Student with inAge=%d and inName=%s.", inAge, inName);
if ('undefined' !== typeof inAge) {
this.setAge(inAge);
}
if ('undefined' !== typeof inName) {
this.setName(inName);
}
}
Student.prototype = new Personne();
Student.prototype.age = undefined;
Student.prototype.setAge = function(inAge) { this.age = inAge; };
Student.prototype.getAge = function() { return this.age; };
// ---------------------------------------------
// We define a Personne.
// ---------------------------------------------
var Personne = function(inName) {
// We define the prototype here.
this.name = undefined;
this.setName = function(inName) { this.name = inName; },
this.getName = function() { return this.name; }
// Do some initialization.
console.log("Executing the constructor Personne with inName=%s.", inName);
if ('undefined' !== typeof inName) {
this.name = inName;
}
}
// ---------------------------------------------
// We define a Student.
// ---------------------------------------------
var Student = function(inAge, inName) {
// We define the prototype here.
Personne.call(this, inName);
this.age = undefined;
this.setAge = function(inAge) { this.age = inAge; };
this.getAge = function() { return this.age; }
// Do some initialization.
console.log("Executing the constructor Student with inAge=%d and inName=%s.", inAge, inName);
if ('undefined' !== typeof inAge) {
this.setAge(inAge);
}
}
if (process.argv.length < 3) {
console.log('Usage: node "%s" <test number (1|2)>', process.argv[1]);
return;
}
var test = process.argv[2];
if (test == 1) {
// -------------------------------------------------
// Executing test 1.
// -------------------------------------------------
console.log("Executing test 1");
(function() {
// ---------------------------------------------
// We define a Personne.
// ---------------------------------------------
var Personne = function(inName) { // This is the constructor.
// Do some initialization.
console.log("Executing the constructor Personne.");
if ('undefined' != typeof inName) {
this.name = inName;
}
}
var PersonnePrototype = { // This is the prototype.
name: undefined,
setName: function(inName) { this.name = inName; },
getName: function() { return this.name; }
};
Personne.prototype = PersonnePrototype;
// ---------------------------------------------
// We define a Student.
// ---------------------------------------------
var Student = function(inAge, inName) { // This is the constructor.
// Do some initialization.
console.log("Executing the constructor Student with inAge=%d and inName=%s.", inAge, inName);
if ('undefined' !== typeof inAge) {
this.setAge(inAge);
}
if ('undefined' !== typeof inName) {
this.setName(inName);
}
}
Student.prototype = new Personne();
Student.prototype.age = undefined;
Student.prototype.setAge = function(inAge) { this.age = inAge; };
Student.prototype.getAge = function() { return this.age; };
var Tom = new Student(12, "Tom");
console.log("The student %s is %d years old.", Tom.getName(), Tom.getAge());
var Joe = new Student();
Joe.setName("Joe");
Joe.setAge(20);
console.log("The student %s is %d years old.", Joe.getName(), Joe.getAge());
console.log(Joe.__proto__);
console.log(Object.getPrototypeOf(Joe));
})();
return;
}
if (test == 2) {
// -------------------------------------------------
// Executing test 2.
// -------------------------------------------------
console.log("Executing test 2");
(function() {
// ---------------------------------------------
// We define a Personne.
// ---------------------------------------------
var Personne = function(inName) {
// We define the prototype here.
this.name = undefined;
this.setName = function(inName) { this.name = inName; },
this.getName = function() { return this.name; }
// Do some initialization.
console.log("Executing the constructor Personne with inName=%s.", inName);
if ('undefined' !== typeof inName) {
this.name = inName;
}
}
// ---------------------------------------------
// We define a Student.
// ---------------------------------------------
var Student = function(inAge, inName) {
// We define the prototype here.
Personne.call(this, inName);
this.age = undefined;
this.setAge = function(inAge) { this.age = inAge; };
this.getAge = function() { return this.age; }
// Do some initialization.
console.log("Executing the constructor Student with inAge=%d and inName=%s.", inAge, inName);
if ('undefined' !== typeof inAge) {
this.setAge(inAge);
}
}
var Tom = new Student(12, "Tom");
console.log("The student %s is %d years old.", Tom.getName(), Tom.getAge());
var Joe = new Student();
Joe.setName("Joe");
Joe.setAge(20);
console.log("The student %s is %d years old.", Joe.getName(), Joe.getAge());
console.log(Joe.__proto__);
console.log(Object.getPrototypeOf(Joe));
})();
return;
}
console.log("Bad test number %d.", test);
执行:
$ node oo.js 1
Executing test 1
Executing the constructor Personne.
Executing the constructor Student with inAge=12 and inName=Tom.
The student Tom is 12 years old.
Executing the constructor Student with inAge=NaN and inName=undefined.
The student Joe is 20 years old.
{ age: undefined, setAge: [Function], getAge: [Function] }
{ age: undefined, setAge: [Function], getAge: [Function] }
和
$ node oo.js 2
Executing test 2
Executing the constructor Personne with inName=Tom.
Executing the constructor Student with inAge=12 and inName=Tom.
The student Tom is 12 years old.
Executing the constructor Personne with inName=undefined.
Executing the constructor Student with inAge=NaN and inName=undefined.
The student Joe is 20 years old.
{}
{}
答案 0 :(得分:3)
在你的例子中,它们确实是等价的。
请注意,在构造函数中定义属性会为对象的每个实例创建它们的副本,但在原型中定义它们只会生成一个副本(原型中的那个)。