gpt4 book ai didi

javascript - 对象构造 : is the prototype really necessary?

转载 作者:行者123 更新时间:2023-11-30 12:29:39 26 4
gpt4 key购买 nike

我正在探索 JavaScript 中的继承概念,但我认为我遗漏了一些东西。

我的目标:我想创建一个继承自另一个对象的对象。JavaScript,我认为我遗漏了一些东西。

例如,我创建了一个继承自对象“Personne”的对象“Student”。

我可以通过两种方式实现这个目标:

  • 第一种方式:我在构造函数的原型(prototype)中声明属性和方法(参见示例 1)。
  • 第二种方式:我在构造函数中声明属性和方法(参见示例 2)。

在我看来,这两种方式都不错。我的意思是:显然,任何一种方法都可以。

但是,如果我看第二种方式,然后我注意到该对象没有原型(prototype)。因此,我想:这正常吗?事实上,我更喜欢第二种方式,因为我觉得它更优雅。

注意:我提供了一个应该与 NodeJs 一起使用的现成脚本。我也给出了执行结果。

所以我的问题是:

  • 这两种方式是否等价?
  • 如果是,那么:“原型(prototype)”属性的用途是什么? (好像按照第二种方式,没有定义)。

谢谢

示例 1:我在构造函数的原型(prototype)中声明属性和方法

// ---------------------------------------------
// 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; };

示例 2:我在构造函数本身中声明属性和方法

// ---------------------------------------------
// 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.
{}
{}

最佳答案

  1. 在您的示例中,它们确实是等价的。

  2. 请注意,在构造函数中定义属性将为对象的每个实例制作一份副本,但在原型(prototype)中定义它们只会制作一份副本(原型(prototype)中的一份)。

关于javascript - 对象构造 : is the prototype really necessary?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28149866/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com