简体   繁体   English

无法理解ECMAScript规范中的短语

[英]Undestanding a phrase from ECMAScript Specification

I'm reading ECMAScript Language Specification and can't understand properly one phrase. 我正在阅读ECMAScript语言规范,无法正确理解一个短语。

The phrase is 这句话是

Every object created by a constructor has an implicit reference ( called the object's prototype ) to the value of its constructor's "prototype" property. 构造函数创建的每个对象都有对其构造函数的“ prototype”属性值的隐式引用( 称为对象的prototype )。

ECMAScript - 4.2.1 Objects - second paragraph ECMAScript-4.2.1对象-第二段

I can't uderstand the phrase in parenthesis called the object's prototype . 我无法理解括号中called the object's prototype的短语。

The phrase in parenthesis tell that a prototype is an implicit reference. 括号中的短语表明原型是隐式引用。

My question is : Prototype is reference? 我的问题是 :原型是参考? An implicit reference is an prototype? 隐式引用是原型吗? Actually? 其实? I always thought that the prototype is the object. 我一直以为原型就是对象。 But ECMA says that an implicit reference to the value of constructor's "prototype" property is object's prototype. 但是ECMA表示,对构造函数的“ prototype”属性值的隐式引用是对象的原型。

I correctly understand what the ECMAScript says, or my understanding is wrong? 我正确理解ECMAScript所说的话,或者我的理解是错误的?

I think we could simply reword it to 我认为我们可以将其改写为

"Every object created by a constructor has an implicit reference to the value of its constructor's "prototype" property, called the object's prototype." “由构造函数创建的每个对象都有对其构造函数的“ prototype”属性值的隐式引用,称为该对象的原型。”

All objects in javascript get their properties and methods from its prototype. javascript中的所有对象都从其原型获取其属性和方法。 Prototype word refers to "a first or preliminary version of something". 原型词是指“某物的第一个或初步版本”。 So, whenever you create anything (class or function) in javascript which can have its object created, its initial skeletal structure can be referred to as a prototype. 因此,每当您在javascript中创建任何可以创建其对象的东西(类或函数)时,其初始骨架结构都可以称为原型。 So, suppose you defined a javascript class with a variable printStatement and a method print(), each object of that class will definitely contain these two things. 因此,假设您定义了一个带变量printStatement和方法print()的javascript类,则该类的每个对象肯定都包含这两件事。 How? 怎么样? from the prototype you created. 从您创建的原型开始。

function Printer(statement) {
    this.printStatement = statement;
    this.print = function()
    {
      // Any send to printer logic
      // this.printStatement.sendToPrinter();
    }
} 

The above statement defines a prototype for Printer function. 上面的语句定义了Printer函数的原型。

You can call it as Printer("Print this line").print(); 您可以将其称为Printer("Print this line").print();

Now, some 50 lines later in your file, you think that this object should have one more function, ReadPrint statement. 现在,在文件的大约50行之后,您认为该对象还应该具有一个功能ReadPrint语句。 You can access the prototype through the object itself and add it in memory. 您可以通过对象本身访问原型并将其添加到内存中。

Printer.prototype.ReadPrint = function(){
      //whatever it does
}

and from the next lines, you can call it as Printer.ReadPrint() 在接下来的几行中,您可以将其称为Printer.ReadPrint()

Since in javascript, prototype is a open available object. 由于在javascript中,prototype是一个开放的可用对象。 You can add your own stuff to its very definition, like 您可以在自己的定义中添加自己的内容,例如

String.prototype.findAllIndexesOf = function(){
     //whatever it does.
}

When you do that, .findAllIndexesOf() will be available on every string object in the following lines and can be called in the same way you would call split(), trim() and other methods on any string object. 当您这样做时, .findAllIndexesOf()将在以下各行中的每个字符串对象上可用,并且可以用与在任何字符串对象上调用split(), trim()和其他方法相同的方式调用。

Do understand that adding functions and properties through this way does not affect the original code, so, if you have to add functions to a prototype of different objects which are available in javascript, add those functions in one file, and you would have to include this file on your pages. 要了解通过这种方式添加函数和属性不会影响原始代码,因此,如果必须将函数添加到javascript中可用的不同对象的原型中,请将这些函数添加到一个文件中,并且必须包括此文件在您的页面上。

See the link below 见下面的链接

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

and check out the Built-in objects section. 并查看“ 内置对象”部分。 It shows available javascript objects and shows usage of prototype. 它显示了可用的javascript对象,并显示了原型的用法。 Click on any object like Array or string, and you will understand the functioning of prototype. 单击任何对象,如数组或字符串,您将了解原型的功能。

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

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