对象构造:原型真的有必要吗?

时间:2015-01-26 12:08:54

标签: javascript inheritance constructor prototype

我正在探索JavaScript中的遗产概念,我认为我错过了一些东西。

我的目标:我想创建一个继承自另一个对象的对象。我认为我错过了一些东西。

作为一个例子,我创建了一个对象"学生"继承自对象" Personne"。

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

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

从我的观点来看,两种方式都很好。我的意思是:显然,无论哪种方式都可以。

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

注意:我提供了一个可以与NodeJs一起使用的准备好的脚本。我也给出了执行结果。

所以我的问题是:

  • 两种方式都相同吗?
  • 如果是,那么:" protitype"的目的是什么?属性? (看来,按照第二种方式,它没有定义)。

由于

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

// ---------------------------------------------
// 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 个答案:

答案 0 :(得分:3)

  1. 在你的例子中,它们确实是等价的。

  2. 请注意,在构造函数中定义属性会为对象的每个实例创建它们的副本,但在原型中定义它们只会生成一个副本(原型中的那个)。