Javascript prototype

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);

Conclusion: June 30

I expected the result should be: June 30th Jonh

but this is not what I expected. So can someone explain to me what is wrong with the above code?

+4
source share
3 answers

You set the property prototypeto an instance created new Employeethat does nothing to connect the prototype chain. The property prototypebecomes a function Employee. This property is then used to set the internal property of [[Prototype]]objects created with new Employee.

So, suppose you want to Employeebe a subclass People, you would do it like this:

// 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` `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);
Run code

, , 2016 , : class ES2015 (, ), :

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);
+13
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);
0

When you speak:

this.prototype = o;

You essentially create a property called "prototype" and assign a value to it. Javascript assumes that you are trying to store values ​​in a variable called "prototype".

If you typed:

print(employee1.prototype.FirstName);

You'll get

Output: John

-3
source

Source: https://habr.com/ru/post/1663486/


All Articles