【问题标题】:javascript prototype propertie hookupjavascript原型属性连接
【发布时间】:2017-04-27 00:55:23
【问题描述】:
var People = function()
{
    this.FirstName = "John";
    this.LastName = "Doer";
}

var Employee = function(o)
{
    this.DateHired = "June 30";
    this.prototype = o;
}

var obj = new People();
var employee1 = new Employee(obj);
print(employee1.DateHired);
print(employee1.FirstName);

输出: 6 月 30 日

我预计输出应该是: 6月30日 乔恩

但这不是我所期望的。那么有人可以解释一下上面的代码有什么问题吗?

【问题讨论】:

  • 您的print()javacript 标签有何关联?更正您的问题
  • 应该是console.log,可能是..
  • 我正在使用 js 交互式控制台,所以 print() 可以工作。
  • @T.J.Crowder 我的目的不是为了上课。我试图从我从书中读到的一段代码中了解原型连接是如何工作的: if (typeof Object.beget !== 'function') { Object.beget = function (o) { var F = function ( ) {}; F.prototype = o;返回新的 F(); }; } var another_stooge = Object.beget(stooge);上面的代码工作。

标签: javascript


【解决方案1】:

您在new Employee 创建的实例 上设置了一个名为prototype 的属性,它不会对原型链进行任何连接。 prototype 属性适用于 函数 Employee。然后使用该属性设置通过new Employee 创建的对象的[[Prototype]] 内部属性。

因此,假设您希望 Employee 成为 People 的子类,您可以这样做:

// The singular is "Person", so I'll use that instead of People
var Person = function() {
  this.FirstName = "John";
  this.LastName = "Doer";
};

var Employee = function() {
  // Give `Person` a chance to do its initialization of the instance
  Person.call(this/*, possible, arguments, here*/);

  // Now carry on with the `Employee` initialization
  this.DateHired = "June 30";
};

// Make Employee a subclass of Person by
// 1. Giving `Employee.prototype` a new object whose [[Prototype]] is
//    `Person.prototype`
// 2. Ensuring that `Employee.prototype`'s `constructor` property points
//    back to Employee.
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;

// Usage
var employee1 = new Employee();
console.log(employee1.DateHired);
console.log(employee1.FirstName);

然而,到了 2016 年底,我们就不需要再玩那些游戏了:我们可以使用 ES2015 的 class 语法(如果我们需要将代码部署到旧浏览器),它为我们处理管道:

class Person {
  constructor() {
    this.FirstName = "John";
    this.LastName = "Doer";
  }
}

class Employee extends Person {
  constructor() {
    // Give `Person` its chance to initialize the instance
    super();
    // Now carry on with the `Employee` initialization
    this.DateHired = "June 30";
  }
}

// Usage
const employee1 = new Employee();
console.log(employee1.DateHired);
console.log(employee1.FirstName);

【讨论】:

  • 也可以添加'People.apply(this, arguments);'在 Employee 函数的开始处。
  • @AliaksiejMaroz:是的,但前提是您需要传递所有参数。通常你会选择。
  • 是的,你是对的。我的示例仅适用于这种特殊情况。
  • @T.J.Crowder 谢谢。现在我明白我的代码问题出在哪里了
【解决方案2】:
Inheritance works as below


    var People = function()
            {
                this.FirstName = "John";
                this.LastName = "Doer";
            }

            var Employee = function()
            {
                this.DateHired = "June 30";
                //this.prototype = o;
                People.call(this);
            }
            Employee.prototype=Object.create(People.prototype);
            var employee1 = new Employee();
            console.log(employee1.DateHired);
            console.log(employee1.FirstName);

【讨论】:

  • 主要问题是这不是继承在JavaScript 中的工作方式。该代码中没有发生继承。您只需在对象上设置属性的功能。 obj 变量也没有任何意义,您不要在 Employee 中使用 o 参数,...
  • @T.J.Crowder:现在还好吗?
  • 现在好多了,但仍然缺少constructor 修复,此时它只是重复我之前的答案,这并不是真的有用,它仍然没有解释任何事情(例如为什么 OP 的代码不起作用,以及为什么这个代码起作用)。
【解决方案3】:

当你说:

this.prototype = o;

您实际上是在创建一个名为“prototype”的属性并为其赋予一个值。 Javascript 假定您正在尝试将值存储在名为“原型”的变量中。

如果您输入:

print(employee1.prototype.FirstName);

你会得到

输出:约翰

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 2013-11-16
    • 1970-01-01
    • 1970-01-01
    • 2016-03-17
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 2015-01-17
    相关资源
    最近更新 更多