简体   繁体   English

对象原型模式

[英]Object.prototype pattern

I do not understant, how to use Object.prototype pattern with classes (es6); 我并不了解如何在类(es6)中使用Object.prototype模式;

This is my code, I'm not sure, that I've used Object.prototype in the right way 我不确定这是我的代码,我以正确的方式使用了Object.prototype

    class Course{ 
     constructor(title, author) {
        this.title = title;
        this.author = author;
      }
    }


    Course.prototype.toString = function (arguments) {
        console.log(this.title + "... Author: " + this.author);
    };

    var course_1 = new Course("Bootstrap 4", "Paul");
    var course_2 = new Course("Design Patterns", "Paul");

    course_1.toString();
    course_2.toString();

}

Should I use something different? 我应该使用其他东西吗?

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Classes

ES6 classes is syntax sugar that allows you to avoid using Object.prototype, you simply define class methods like this: ES6类是语法糖,它使您可以避免使用Object.prototype,只需定义如下的类方法即可:

    class Course{ 
         constructor(title, author) {
            this.title = title;
            this.author = author;
          }

         toString(arguments) {
            console.log(this.title + "... Author: " + this.author);
        }
    }

Writing an application using es6 classes is an alternative to developing using the prototype pattern directly. 使用es6类编写应用程序是直接使用原型模式进行开发的一种替代方法

Under the hood, es6 classes actually compile down to the prototype structure. 实际上,es6类实际上可以编译为原型结构。 But es6 classes tend to be easier to read, and when your application gets very large this can make all the difference. 但是es6类往往更易于阅读,当您的应用程序变得很大时,这可能会有所作为。

In your case you would place the methods you want attached to the prototype inside the class you've created. 在您的情况下,您可以将要附加到原型的方法放在创建的类中。 This looks more similar to classic object oriented programming, as you would see in C++ or Java. 正如您在C ++或Java中看到的那样,这看起来更类似于经典的面向对象的编程。

You can read more about es6 classes on MDN here 您可以在此处阅读有关MDN上的es6类的更多信息

Per your example: 根据您的示例:

class Course { 
 constructor(title, author) {
    this.title = title;
    this.author = author;
  }

  toString(arguments) {
     console.log(this.title + "... Author: " + this.author);
  }
}

var course_1 = new Course("Bootstrap 4", "Paul");
var course_2 = new Course("Design Patterns", "Paul");

course_1.toString();
course_2.toString();

In your following code: 在您的以下代码中:

 class Course { constructor(title, author) { this.title = title; this.author = author; } } Course.prototype.toString = function(arguments) { console.log(this.title + "... Author: " + this.author); }; var course_1 = new Course("Bootstrap 4", "Paul"); var course_2 = new Course("Design Patterns", "Paul"); course_1.toString(); course_2.toString(); 

A class is nothing more than syntactic sugar which resembles the functionality of a constructor function. 类只不过是语法糖,类似于构造函数的功能。 We can observe this more depth in the following example: 在以下示例中,我们可以观察到更多的深度:

 class Person { } console.log(typeof Person); 

The class Person is actually a constructor function object. Person类实际上是一个构造函数对象。 Just like normal constructors functions we can extend the prototype by putting properties on the prototype object. 就像普通的构造函数一样,我们可以通过将属性放在原型对象上来扩展原型。

So in your example: 因此,在您的示例中:

Course.prototype.toString = function(arguments) {
  console.log(this.title + "... Author: " + this.author);
};

What actually happens under the hood is that you are putting a property called toString on the Course constructor function object. 实际发生的情况是,您将名为toString的属性放在Course构造函数对象上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM